[Rails] Re: [Beginner] fields_for within each loop

2011-10-20 Thread Hakjune Kim
Thank you Fred for another reply. But having relationship report->question->answer will lead to unnecessary generation of the question per each report. In my case, question is same for all reports. Could you please guide me what I should do then? I feel like I'm stuck in db model now. Your adv

[Rails] Re: [Beginner] fields_for within each loop

2011-10-19 Thread Hakjune Kim
It doesn't work as I expected. I changed the controller and view but it doesn't work in report_controller.rb def new ... QuestionSingle.each do |qs| report.answer_singles.build ... end so answer_single generated as many as questions. So when I call answer_singles in the fields for it c

[Rails] Re: [Beginner] fields_for within each loop

2011-10-19 Thread Hakjune Kim
Thank you very much Frederick. I have changed my code according to it. But in the form how I can implement this? Once I'm using the same code it will display 3 fields since it's using fields_for loop for all answers. What can I use in this case? -- Posted via http://www.ruby-forum.com/. -- Y

[Rails] Re: [Beginner] fields_for within each loop

2011-10-19 Thread Hakjune Kim
I found from the development log that I'm passing question_single_id as nil. Seems to be my code of this part is not working <%= f.fields_for :answer_singles, {:question_single_id => index} do |answer| %> <%= answer.text_area :content, {:question_single_id => index} %> What I meat to do wa

[Rails] Re: [Beginner] fields_for within each loop

2011-10-19 Thread Hakjune Kim
Sure, Thank you very much. reports_controller.rb def new @report = Report.new @report.answer_singles.build respond_to do |format| format.html # new.html.erb format.json { render :json => @report } end end _form.html.erb <%= form_for(@report) do |f| %> <% if @repo

[Rails] Re: [Beginner] fields_for within each loop

2011-10-19 Thread Hakjune Kim
By making it nested_attributes I'm able to control it but still some weird loop. I don't fully understand how loop works in rails so please advise. First this code makes one text area per each question. <% QuestionSingle.all.each_with_index do |question, index| %> <%= index+1 %>. <%=