问题描述:

I'm trying to get a user's quiz answers saved to a database. This presumably should happen when they click the 'get my results' button, but prior to that - how do I 'link' a question to the appropriate value in the database?

Here is my example quiz form partial (which gets rendered in my page view):

<%= form_for([current_user]) do |f| %>

<h3>1. I have the final say in decisions made within the group I'm leading.</h3>

<div class="btn-group-vertical clearfix form-group" data-toggle="buttons">

<label class="btn btn-primary text-left">

<input name="options" id="option1" type="radio" onclick="multiChoiceClick(1, 1)">A. Always. I'm the leader and should have the final say

</label>

<label class="btn btn-primary text-left">

<input name="options" id="option2" type="radio">B. Sometimes, but I think the group should make decisions if possible

</label>

<label class="btn btn-primary text-left">

<input name="options" id="option3" type="radio">C. I generally don't get involved. The group can make their own decisions without my help

</label>

</div>

<p>

<%= f.submit("Get my results!") %>

</p>

<% end %>

Just as an example, how do I get this question linking to the database?

I'm using the Devise gem to get users logged in and out (that's all working fine). My User model has_many :quiz_answers and the QuizAnswer model belongs_to :user.

Can anyone tell me the immediate next step?

网友答案:

I think the next step is to use fields_for :quiz_answers in the form builder. In your User model you will have to add accepts_nested_attributes_for :quiz_answers. And in your controller you need to permit the relevant parameters. Also see the guide for Nested Forms.

相关阅读:
Top