Hi, I''m trying to insert 4 records into a table named
''answers''.
so in my form, i''m doing this:
<% 0.upto(3) do |i| %>
<tr>
<td>
<label for="answer_answer">Answer</label><br/>
<%= text_field ''answer'', ''answer['' +
i.to_s + '']'' %>
</td>
<td>
<label
for="answer_correct">Correct?</label><br/>
<%= check_box ''answer'', ''correct['' +
i.to_s + '']'' %>
</td>
</tr>
<% end %>
which gives me the following html:
<tr>
<td>
<label for="answer_answer">Answer</label><br/>
<input id="answer_answer[0]" name="answer[answer[0]]"
size="30"
type="text" />
</td>
<td>
<label
for="answer_correct">Correct?</label><br/>
<input id="answer_correct[0]"
name="answer[correct[0]]"
type="checkbox" value="1" /><input
name="answer[correct[0]]"
type="hidden" value="0" />
</td>
</tr>
in my controller, i have this as the create method:
def create
@course = Course.new(params[:course])
@question = Question.new(params[:question])
@answer = Answer.new(params[:answer])
if @course.save
flash[:notice] = ''Course was successfully created.''
flash[:course_id] = @course.id
flash[:answers] = @answer
@question.course_id = @course.id
@question.save
@answer.course_id = @course.id
@answer.question_id = @question.id
@answer.save
redirect_to (:controller => ''courses'', :action =>
''list'')
else
render :action => ''new''
end
end
the error i am getting is: undefined method `correct[3]='' for
#<Answer:0x4733418>
and these are the parameters being passed:
Parameters: {"commit"=>"Create",
"answer"=>{"correct[3]"=>"0",
"correct[2]"=>"0",
"answer[3]"=>"purple",
"correct[1]"=>"0",
"answer[2]"=>"green",
"correct[0]"=>"1",
"answer[1]"=>"red",
"answer[0]"=>"blue"},
"question"=>{"question"=>"What color are my
shoes.", "feedback"=>"My shoes are blue."},
"course"=>{"game"=>"0",
"title"=>"passing more data",
"code"=>"TST1008", "lib"=>"0",
"description"=>"just another test"}}
I have the agile book, and i see that I can pass create() an array of
attribute hashes, but I''m at a loss as to how to get those parameters
into an appropriate array for create().
Any help will be much appreciated. I am a ruby nuby, so go slow.
Thanks, Ian
--
--
Ian Kennedy
http://www.fiftymillimeter.com
If "correct" is not an active record attribute, you need to define accessors for "correct". Ian Kennedy wrote:>Hi, I''m trying to insert 4 records into a table named ''answers''. > >so in my form, i''m doing this: > ><% 0.upto(3) do |i| %> ><tr> > <td> > <label for="answer_answer">Answer</label><br/> > <%= text_field ''answer'', ''answer['' + i.to_s + '']'' %> > </td> > <td> > <label for="answer_correct">Correct?</label><br/> > <%= check_box ''answer'', ''correct['' + i.to_s + '']'' %> > </td> ></tr> ><% end %> > >which gives me the following html: > ><tr> > <td> > <label for="answer_answer">Answer</label><br/> > <input id="answer_answer[0]" name="answer[answer[0]]" size="30" >type="text" /> > </td> > <td> > <label for="answer_correct">Correct?</label><br/> > <input id="answer_correct[0]" name="answer[correct[0]]" >type="checkbox" value="1" /><input name="answer[correct[0]]" >type="hidden" value="0" /> > </td> ></tr> > >in my controller, i have this as the create method: > > def create > @course = Course.new(params[:course]) > @question = Question.new(params[:question]) > @answer = Answer.new(params[:answer]) > if @course.save > flash[:notice] = ''Course was successfully created.'' > flash[:course_id] = @course.id > flash[:answers] = @answer > @question.course_id = @course.id > @question.save > @answer.course_id = @course.id > @answer.question_id = @question.id > @answer.save > redirect_to (:controller => ''courses'', :action => ''list'') > else > render :action => ''new'' > end > end > >the error i am getting is: undefined method `correct[3]='' for >#<Answer:0x4733418> > >and these are the parameters being passed: > >Parameters: {"commit"=>"Create", "answer"=>{"correct[3]"=>"0", >"correct[2]"=>"0", "answer[3]"=>"purple", "correct[1]"=>"0", >"answer[2]"=>"green", "correct[0]"=>"1", "answer[1]"=>"red", >"answer[0]"=>"blue"}, "question"=>{"question"=>"What color are my >shoes.", "feedback"=>"My shoes are blue."}, "course"=>{"game"=>"0", >"title"=>"passing more data", "code"=>"TST1008", "lib"=>"0", >"description"=>"just another test"}} > >I have the agile book, and i see that I can pass create() an array of >attribute hashes, but I''m at a loss as to how to get those parameters >into an appropriate array for create(). > >Any help will be much appreciated. I am a ruby nuby, so go slow. > >Thanks, Ian > >-- >-- >Ian Kennedy >http://www.fiftymillimeter.com >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >
>> If "correct" is not an active record attribute, you need to define >> accessors for "correct".Bruce, correct is an AR attribute, or should be. If I rewrite the code to insert a single record, like below, I have no troubles. <tr> <td> <label for="answer_answer">Answer</label><br/> <%= text_field ''answer'', ''answer'' %> </td> <td> <label for="answer_correct">Correct?</label><br/> <%= check_box ''answer'', ''correct'' %> </td> </tr> So, I''m thinking that I need to do something in the controller along the lines of: @answers = params[:answer] @answers.each do |a| Answer.create ( { :answer => a.answer, :correct => a.correct, :course_id => @course.id, :question_id => @question.id } end But I''m new to this and feel like there should be an easy ruby/rails way about inserting multiple records. Any suggestions? Thanks, Ian -- -- Ian Kennedy http://www.fiftymillimeter.com