views:

62

answers:

2

Hi All,

I have 4 models - Users, Lessons, Questions & Answers. Each user can create a lesson with some questions and then ask other users to answer those questions and submit the form. I ran into a problem creating a view to display a lesson with a list of questions and a blank answer field underneath each question. I have a working code (shown here) that loops through questions and shows a text field for each question and answer. I am trying to change it so that the questions are shown as headers and answers are shown as editable fields. I hope this makes sense. I am a noob with RoR. I couldn't find an answer online. Thank you so much.

--View

<% form_for @lesson do |f| %>
 <%= f.error_messages %>
  <% f.fields_for :questions do |builder| %>
   <%= render "question_fields", :f => builder %>
  <% end %>
 <p><%= f.submit "Submit Answers"%>
<% end %>

--partial _question_fields.html.erb

<%= f.text_area :prompt, :rows => 1 %> <br />
<% f.fields_for :answers do |builder| %>
 <%= render "answer_fields", :ff => builder %>
<% end %><br />

--partial _answer_fields.html.erb

<%= ff.text_area :data, :rows => 3 %>
A: 

If I understand what you are trying to do, I believe you need to change the line in _question_fields.html.erb:

<%= f.text_area :prompt, :rows => 1 %> <br />

to this:

<%= f.label :prompt %> <br />

This will make the question show up on top of the text area for the answer. This code will definitely make sure the user cannot change the question. If you want the question field to remain a text area but be disabled, then I think you need to do this:

<%= f.text_area :prompt, :rows => 1, :disabled => "disabled" %> <br />

I'm not sure if the disabled part is correct, but it is something like that.

Hope this helps.

Snapman
Snapman, thanks. Changing <%= f.text_area :prompt, :rows => 1 %> to <%= f.label :prompt %> doesn't write the contents of the field, it writes the name of the field - "prompt". You are on the right track. Anyone else? This seems to be a common issue, no?
Alex
Ah, well the argument to f.label is expecting a method call on a model, so if prompt isn't the correct method, then you need prompt_id or prompt.value or something like that.
Snapman
<%= f.object.prompt %> Worked
Alex
A: 

You should be able to access the "question object" itself like so:

<%= f.object.prompt %>

Which should output the "prompt" field of the question object passed into fields_for. I'm not sure if you can combine that with <%= f.label %> or not.

Best of luck!
~Robbie

Robbie
Robbie, That worked! Thanks so much.
Alex