last day (14 days later) » 

07:59
0
Q: Building survey from railscast in rails 5

Caffeine CoderI have been following both the old and revised railscasts & this for something that i have to make along the same lines I followed it upto some point but neither are the questions being displayed on the form nor are the answers getting added . Following is my model code answers.rb class Answer

Can you post your survey view forms? It looks like you might be expecting nested_attributes, but you aren't white-listing them in your permit call, so want to double check your forms are setup correctly
Updated , please check .
The main issue arises in new method of survey controller , this particular line - 4.times { question.answers.build }
Oh so you're not even getting to the view, okay...you have run migrations and everything, correct?
Yes , i just have two migrations of survey and questions
Updated the same
So you don't have a migration creating an answers table? You'll need to create a table for answers
07:59
Hey
I did think on the same , but there weren't any answers migration there
Hey here we go, I was trying to figure out how to invite you to a chat room and then it automatically asked me if I wanted to ask you
Haha
Yes , SO does that
if you extend to more than 10 comments
Ah alright, only been a member for like 2 days, still getting the ropes...but let me scan through your linked tutorials real fast
Sure , thanks for helping out . Really glad someone chipped in .
Ah, so here we go...in that first one, #196 in the first code block down in the Show Notes it has 'script/generate model answer question_id:integer content:string'
08:02
must have missed that
which would nowadays be 'rails g model answer question_id:integer content:string' and that creates a model file and a database migration for you
let me check that up on app real fast
ok , that's gone but a new issue came which a bit weird
undefined local variable or method `f'
in _form.html.erb
The code in the form field
<%= f.fields_for :questions do |builder| %>
<%= render 'question_fields', f: builder %>
<% end %>
<%= link_to_add_fields "Add Question", f, :questions %>
okay, so for the form partial, it looks like you were supposed to start with the file from the un-revised tutorial and then update that code with the code from the file in the revised partial...what you pasted in the question is just the code from the revised tutorial
<%= form_for @survey do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
so that's the original file, so what you have looks like it should just be added near the end (the last 3 or so lines before the submit button)
I had done that before updating the code for revised part , put yours one in
undefined method `error_messages' for #<ActionView::Helpers::FormBuilder:0x000000060b8118>
this shows up
Okay looks like a mistake on his part then, or just explained something during the videos that we're missing glancing through his Notes, just go ahead and remove that line causing the errors
that method looks like it was deprecated years ago
08:12
Missing partial surveys/_answer_fields, application/_answer_fields with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in:
after removing the error message one
whereas i do have an answer field partial
inside surveys folder view
in your question you pasted the partial in as '_answers_fields.html.erb' (answers is plural and your calling it in the render as plural)
so go ahead and double check spelling and such on that one
you're calling it in the render as singular*
yes
that was the mistake
it's working now
Though need to check the show method too
on clicking edit it isn't showing all the questions i entered
Any ideas ?
Even questions and answers are not getting reflected on db
is it only the questions/answers you added via that javascript 'add question'/'add answer' link or questions/answers that are on the page when you loaded it?
08:20
i added the coffee code that's listed here railscasts.com/episodes/…
so the view shows up fine
on submit it says that survey is created
but the question and answers don't get loaded up neither in db nor view
right right, you mentioned some of the questions didn't save...were the questions added into fields added onto the page by clicking "Add Question" or did questions that were put into fields that were rendered from the server get messed up as well?
Also, can you past the parameters line of the log file for that post?
sure , gimme a minute
Started POST "/surveys" for 127.0.0.1 at 2017-07-08 13:53:11 +0530
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oJ8Adwr9d49uMIW3Ox6jxV2ifmlcDaNtSBKUky8UBxUAKxbixRXz7tAv3QIly9pVbeezzv7DRzT9g6SCfKAY0Q==", "survey"=>{"name"=>"aaaa", "questions_attributes"=>{"0"=>{"content"=>"aaaaaaa", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"content"=>"bbbb", "_destroy"=>"0"}, "1"=>{"content"=>"cccccc", "_destroy"=>"0"}, "2"=>{"content"=>"ddddd", "_destroy"=>"0"}, "3"=>{"content"=>"eeeee", "_destroy"=>"0"}}}, "1"=>{"content"=>"ffffffffffffffffff", "_dest
i see it right there
unpermitted paramter
yeah, was just typing that up myself
i had written questions_attributes
Also, in your survey_params method you aren't permitting the *_attributes through to the model
08:26
Didn't get you
What else should i be doing
looking up the correct format, gimme a second
def survey_params
params.require(:survey).permit(:name, :question_attributes)
end
sure
actually that might work, not sure if we'll need to permit the children of question attributes if we're allowing everything in question_attributes
yes , that's what puzzles me .
Okay, so if that's still not working let's try permit(:name, question_attributes: [:content, :_destroy, answer_attributes: [:content, :_destroy]]) though, not gonna lie, I'm in pure guess and check mode here
oh, add :id into both the *_attributes arrays
08:32
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qY1yU0O9d8wFYUx5NlxGkM9vXydb+WWQLG+uLcfJGDkJOWTGjFXzrbt+FMwoiT8A/yqSgPk3gcmZ/p48lH0H/Q==", "survey"=>{"name"=>"tanu", "questions_attributes"=>{"0"=>{"content"=>"aaaaaaaaa", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"content"=>"bbbbbbbbbbb", "_destroy"=>"0"}, "1"=>{"content"=>"ccccccccc", "_destroy"=>"0"}, "2"=>{"content"=>"dddddddddddd", "_destroy"=>"0"}, "3"=>{"content"=>"eeeeeeeeeeee", "_destroy"=>"0"}}}, "1"=>{"content"=>"ffffffffffff", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"conten
shud it be questions or question ,i tried both ...error remains the same
params.require(:survey).permit(:name, question_attributes: [:content, :_destroy, answer_attributes: [:content_id, :_destroy_id]])
i have this now
okay I think since you has_many :questions and has_many :answers you'll need questions_attributes and answers_attributes based on the nested attributes section of edgeapi.rubyonrails.org/classes/ActionController/…
Got it
working now
Oh cool, what was it?
The issue was i wasnt rejecting blanks
i created a proc to reject blank fields
08:36
Can you summarise the changes in your answer so i can select it ?
Thanks a ton :-)
said a bit too early i guess , the questions and answers are still not getting added to db
:-(
Started POST "/surveys" for 127.0.0.1 at 2017-07-08 14:10:33 +0530
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bTLXa6gqY+0VWUJTpZuHiazDLJs0nuWTDRqZ8oMblunNhsH+Z8LnjKtGGua7Tv4ZnIbhPJZQAcq4i6nj0K+JLQ==", "survey"=>{"name"=>"pop", "questions_attributes"=>{"0"=>{"content"=>"dagdfg", "_destroy"=>"0"}, "1"=>{"content"=>"werwerwer", "_destroy"=>"0"}, "2"=>{"content"=>"dfgdfgdgfdg", "_destroy"=>"0"}}}, "commit"=>"Submit"}
(0.1ms) BEGIN
(0.1ms) ROLLBACK
Okay, so still not actually working
did it work for some of them but then stop working/not work for all of them?
or just never worked
and you've removed all the attr_accessor code from all 3 models, correct? it looks like the actual survey object error'd out on that last request, because it wasn't even inserted, which is a step back
09:04
i removed the accssor code
just never got inserted
the question and answer tables are blank
the survey table takes in data though
what's your create method in the controller look like? Also are there any validations except from the reject blank stuff?
and yeah, the survey table has been getting inserts, but that last log you pasted doesn't look like it inserted that one
Processing by SurveysController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"GkowBuIX97A98Koj0ZpDrB3Mj3RaHAh4MemjQ5wAwre6/iaTLf9z0YPv8pbPTzo8LYlC0/jS7CGEeJNSz7Tdcw==", "survey"=>{"name"=>"pop", "questions_attributes"=>{"0"=>{"content"=>"dagdfg", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"content"=>"gggggggggg", "_destroy"=>"0"}, "1"=>{"content"=>"ffffffffff", "_destroy"=>"0"}, "2"=>{"content"=>"dddddddddddddd", "_destroy"=>"0"}, "3"=>{"content"=>"ssssssssssss", "_destroy"=>"0"}}}, "1"=>{"content"=>"werwerwer", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"content"=>"
is it because i added id to it ?
oh sorry guess I was unclear where to add the id, I think it should look like `params.require(:survey).permit(:name, :question_attributes: [:id, :content, :_destroy, answer_attributes: [:id, :content, :_destroy])
`
I think you need the :id attribute passed in so you can update once the create stuff works; didn't mean to append them to the answer_attributes keys
still the same
rollback
def create
@survey = Survey.new(survey_params)
# @form = @survey.questions.create(params[:survey_params])

respond_to do |format|
if @survey.save
format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
format.json { render :show, status: :created, location: @survey }
else
format.html { render :new }
format.json { render json: @survey.errors, status: :unprocessable_entity }
end
end
end
are you getting the line of procession of "Unpermitted parameters"?
09:13
Nope , that's gone
Let me post the whole survey controller
class SurveysController < ApplicationController
before_action :set_survey, only: [:show, :edit, :update, :destroy]

# GET /surveys
# GET /surveys.json
def index
@surveys = Survey.all
end

# GET /surveys/1
# GET /surveys/1.json
def show
end

# GET /surveys/new
def new
@survey = Survey.new
3.times do
question = @survey.questions.build
4.times { question.answers.build }
end
end

# GET /surveys/1/edit
def edit
end

# POST /surveys
# POST /surveys.json
def create
@survey = Survey.new(survey_params)
# @form = @survey.questions.create(params[:survey_params])
Alright, but still no survey or questions/answers in the db
nope , nothing
Okay, open up a console in the terminal bin/rails c
there
Survey.create
?
are you able to create a simple survey object from here? and go through the associations and create questions and answers?
09:18
Survey.create
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `surveys` (`created_at`, `updated_at`) VALUES ('2017-07-08 09:18:32', '2017-07-08 09:18:32')
(41.0ms) COMMIT
=> #<Survey id: 11, name: nil, created_at: "2017-07-08 09:18:32", updated_at: "2017-07-08 09:18:32">
Okay what about survey = Survey.create({
name: "pop",
questions_attributes: [
{ content: "First question" }
]
})
might need to be survey = Survey.create({
name: "pop",
questions_attributes: {
0: { content: "First question" }
}
})
not entirely sure, never tried this on the console
Survey.create({
2.3.1 :005 > name: "pop",
2.3.1 :006 > questions_attributes: [
2.3.1 :007 > { content: "First question" }
2.3.1 :008?> ]
2.3.1 :009?> })
(0.1ms) BEGIN
(0.1ms) ROLLBACK
=> #<Survey id: nil, name: "pop", created_at: nil, updated_at: nil>
2.3.1 :010 >
are there any errors on the survey object?
okay, what about Question.create and Answer.create
let's make sure we can even do anything with those objects
09:23
Questions.create
NameError: uninitialized constant Questions
the model will be singular
Question.create instead of Questions.create
Answer.create
(0.1ms) BEGIN
(0.1ms) ROLLBACK
=> #<Answer id: nil, question_id: nil, content: nil, created_at: nil, updated_at: nil>
2.3.1 :013 > Question.create
(0.1ms) BEGIN
(0.1ms) ROLLBACK
=> #<Question id: nil, survey_id: nil, integer: nil, content: nil, created_at: nil, updated_at: nil>
2.3.1 :014 >
i think these have issues
what about question = Question.new;question.valid? question.errors
what does that show
Question.new
=> #<Question id: nil, survey_id: nil, integer: nil, content: nil, created_at: nil, updated_at: nil>
question = Question.new;question.valid? question.errors
=> false
oh missed a semicolon (sorry it's late out here) question.valid? ; question.errors
09:27
I understand , no worries
2.3.1 :016 > question = Question.new;question.valid?;question.errors
=> #<ActiveModel::Errors:0x00000009298278 @base=#<Question id: nil, survey_id: nil, integer: nil, content: nil, created_at: nil, updated_at: nil>, @messages={:survey=>["must exist"]}, @details={:survey=>[{:error=>:blank}]}>
questions and answers are tied to survey
so they cant be created without a survey id
right makes sense okay, so let's try
survey = Survey.new({
name: "pop",
questions_attributes: [{ content: "First question" }]
})

survey.valid?
survey.errors

question.valid?
question.errors

survey.save
My current thinking is that the code might be trying to save the Question object before the Survey object got saved, so the question doesn't end up with a survey id
2.3.1 :017 > survey = Survey.new({
2.3.1 :018 > name: "pop",
2.3.1 :019 > questions_attributes: [{ content: "First question" }]
2.3.1 :020?> })
=> #<Survey id: nil, name: "pop", created_at: nil, updated_at: nil>
2.3.1 :021 >
2.3.1 :022 > survey.valid?
=> false
2.3.1 :023 > survey.errors
=> #<ActiveModel::Errors:0x000000067f6790 @base=#<Survey id: nil, name: "pop", created_at: nil, updated_at: nil>, @messages={:"questions.survey"=>["must exist"]}, @details={:"questions.survey"=>[{:error=>:blank}]}>
But questions.build is called on survey
Okay, you added a validates_presence_of :survey to the Question model?
so questions can be created only when they have a survey
no , let me do that
reading api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/… looks like you'll need to make sure you have has_many :questions, inverse_of: :survey in the Survey model and then belongs_to :survey, inverse_of: :questions
that's what is mentioned for "validating the presence of a parent model" section
so you've got something set up somewhere to validate the presence of a survey on the Question, which is preventing the question from being created without a survey
09:37
answer.rb shudnt have any inverse , right ?
Ah, it looks like in rails 5 belongs_to automatically marks the field as required
i wrote inverse_of in survey and question
answer.rb would need a belongs_to :question, inverse_of: :answers and question would need has_many :answers, inverse_of: :question
Take a look at the api link I just pasted and search for "validating presence of a parent model" if that's not clear...it's a little murky typed into these chat boxes, imo
did that
this is hoq questions looks like now
since rails 5 apparently requires a belongs_to association to be met, I'm betting big on add "inverse_of" to be the solution we need
09:42
class Question < ApplicationRecord
# attr_accessor :content, :survey_id, :answers_attributes
belongs_to :survey
has_many :answers, inverse_of: :questions
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ActiveRecord::Base
# attr_accessor :content, :question_id
belongs_to :question, inverse_of: :answers
end
class Survey < ApplicationRecord
# attr_accessor :name, :questions_attributes
has_many :questions, inverse_of: :survey
accepts_nested_attributes_for :questions, allow_destroy: true
end
This is what i get - Could not find the inverse association for answers (:questions in Answer)
Okay in Question, "belongs_to :survey, inverse_of: :questions"
so i think answer will have inverse_of question
yeah "has_many :answers, inverse_of: :question" in Question as well
class Question < ApplicationRecord
# attr_accessor :content, :survey_id, :answers_attributes
belongs_to :survey, inverse_of: :questions
has_many :answers, inverse_of: :questions
accepts_nested_attributes_for :answers, allow_destroy: true
end
Could not find the inverse association for answers (:questions in Answer)
class Question < ApplicationRecord
belongs_to :survey
has_many :answers, inverse_of: :question
accepts_nested_attributes_for :answers, allow_destroy: true
end
09:48
ok
woops
class Question < ApplicationRecord
belongs_to :survey, inverse_of: :questions
has_many :answers, inverse_of: :question
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ApplicationRecord
belongs_to :question, inverse_of: :answers
end
Note that Answer should also inherit from ApplicationRecord
from what I'm seeing it's not a big deal, but it's the new norm
class Survey < ApplicationRecord
has_many :questions, inverse_of: :survey
accepts_nested_attributes_for :questions, allow_destroy: true
end
so the inverse_of is specifying the same name as the association in the other model (if that makes sense/clears anything up)
IT GOT CREATED
finally
and data is coming into db
Lesson learnt , need to look into this inverse_of
Alright sorry that took so long to get hammered out
09:52
Had u not helped , this wudnt had gone anywhere and maybe wud had taken a day more to me
Thanks again
np, take it easy

last day (14 days later) »