Rails:如何将html多项选择题与数据库值相关联

时间:2014-12-05 11:27:35

标签: ruby-on-rails ruby devise formbuilder

我试图将用户的测验答案保存到数据库中。这可能是在他们点击“获取我的结果”时发生的。按钮,但在此之前 - 我如何链接'一个问题到数据库中的适当值? 这是我的示例测验形式部分(在我的页面视图中呈现):

<%= 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 %>

举个例子,如何将此问题链接到数据库?

我使用Devise gem来让用户登录和退出(这一切都正常)。我的用户模型has_many:quiz_answers和QuizAnswer模型belongs_to:user。

有人可以告诉我下一步吗?

1 个答案:

答案 0 :(得分:0)

我认为下一步是在表单构建器中使用fields_for :quiz_answers。在User模型中,您必须添加accepts_nested_attributes_for :quiz_answers。在您的控制器中,您需要允许相关参数。另请参阅Nested Forms指南。