红宝石版本L 2.0
导轨版本:4.0
我很难理解我认为应该是一个简单的问题。
我有 2 个控制器:Quizes
我Questions
想关联。为此,我创建了一个模型:assignment
.
测验.rb
class Quiz < ActiveRecord::Base
has_many :assignments
has_many :questions, :through => :assignments
end
问题.rb
class Question < ActiveRecord::Base
has_many :assignments
has_many :quizzes, :through => :assignments
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers, :allow_destroy => true
end
赋值.rb
class Assignment < ActiveRecord::Base
belongs_to :question
belongs_to :quiz
end
作业迁移
class CreateAssignments < ActiveRecord::Migration
def up
create_table :assignments do |t|
t.integer :quiz_id
t.integer :question_id
t.timestamps
end
add_index :assignments, :quiz_id
add_index :assignments, :question_id
end
def down
drop_table :assignments
end
end
我对此很陌生,但我认为我正确地遵循了文档。我现在的问题是,我如何才能真正为用户完成这项工作。我希望用户在创建或编辑测验时能够看到所有问题的列表,并且能够为他们希望与测验关联的每个问题选中一个框。这可能吗?
更新 1 (到目前为止我所拥有的)
/views/quizzes/_form.html.erb
<%= form_for(@quiz) do |f| %>
<% if @quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@quiz.errors.count, "error") %> prohibited this quiz from being saved:</h2>
<ul>
<% @quiz.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :text %><br>
<%= f.text_area :text %>
</div>
<%= fields_for :questions do |quiz_question| %>
<% @questions.each do |question| %>
<%= quiz_question.label question.text %>
<%= check_box_tag :question, question.id %><br>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>