Reputation: 3724
I have two models: CompanyQuestion and UserAnswer
CompanyQuestion has_many UserAnswers, and UserAnswers belongs_to CompanyQuestion
CompanyQuestion has a :question attribute, "The person I'd most like to meet..."
UserAnswer has :answer and :company_question_id attributes.
On the User's profile, I'd like to loop through all the CompanyQuestions with textareas for the User to enter their answer and then submit their answers.
===
What's your favorite book? [ TEXTAREA ]
Who is the most famous person you've met? [ TEXTAREA ]
===
I've accomplished the looping but I'm not sure how to configure the form helper to follow the Rails way of updating the UserAnswer models.
<% @questions.each do |question| %>
<%= form_for question do |form| %>
<div class="form-group">
<%= form.label question.question %>
<%= form.text_area ... %>
</div>
<% end %>
<% end %>
Upvotes: 0
Views: 79
Reputation: 3724
For future reference, I was able to get this to work using:
<% @questions.each do |question| %>
<% @answer = UserAnswer.find_by('user_id = ? AND company_question_id = ?', current_user.id, question.id) %>
<%= form_with(model: @answer, local: true) do |form| %>
<div class="form-group">
<%= form.label question.question %>
<%= form.text_area :answer, class: 'form-control' %>
</div>
<% end %>
<% end %>
I won't mark this as answered for a while in case someone recommends a better convention I should follow.
Upvotes: 0