Ruby on rails Rails 4使用联接表的record.id创建记录
RubyonRails4 我试图让一个表单在我的测试表中创建一个记录。该记录需要具有其联接表中名为questions\u tests的id。我是否需要从表格中创建问题测试中的记录,然后创建测试记录?你会怎么做 模型(不确定我是否正确命名了联接表): 该表格应填写测试属性,并以某种方式具有来自试题和测试的id。现在我不知道如何发送或创建问题\u测试记录。 我的表单,不知道如何选择问题并将其存储到测试记录中。这里,:question_id未定义,但我需要在测试中存储其中的2到200个Ruby on rails Rails 4使用联接表的record.id创建记录,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,RubyonRails4 我试图让一个表单在我的测试表中创建一个记录。该记录需要具有其联接表中名为questions\u tests的id。我是否需要从表格中创建问题测试中的记录,然后创建测试记录?你会怎么做 模型(不确定我是否正确命名了联接表): 该表格应填写测试属性,并以某种方式具有来自试题和测试的id。现在我不知道如何发送或创建问题\u测试记录。 我的表单,不知道如何选择问题并将其存储到测试记录中。这里,:question_id未定义,但我需要在测试中存储其中的2到200个 <h1&
<h1>New Test</h1>
<%= form_for @test do |f| %>
<%= f.label :name, "Test Name" %><br>
<%= f.text_field :name, class: "input-lg" %>
<%= f.label :description, "Description" %><br>
<%= f.text_field :description, class: "input-lg" %>
<%= f.label :question_id %><br>
<%= f.select :question_id, Question.all.collect { |p| [ p.content, p.id ] }, class: "input-lg" %>
<%= f.label :category %><br>
<%= f.select :category, [ ["IP Voice Telephony", "ip_voice"], ["IP Video Surveillance", "ip_video_surveillance"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"] ], {prompt: "Select Category"}, class: "input-lg" %>
<%= f.label :type %><br>
<%= f.select :type, [ ["Beginner", "beginner"], ["Intermediate", "intermediate"], ["Advanced", "advanced"] ], {prompt: "Select Type"}, class: "input-lg" %>
<br/><br/><br/>
<%= f.submit "Create Test", class: "btn btn-lg btn-primary" %>
<% end %>
新测试
您需要查看的字段
表单中将有一个用于
块的字段
f.fields_for :questions do |question|
question.select(...)
end`
在这个模块中,您可以选择要添加到测试中的问题。
有一个名为gem的工具可以帮助您添加“添加问题”链接以添加更多问题
您必须在测试模型中为以下问题添加接受\u嵌套的\u属性\u
。然后Rails将自己创建QuestionTest
,您无需担心。所以我不应该在数据库中创建questions\u tests表?不,您需要一个。Rails只是隐藏了创建这些联接表记录的“复杂性”,但它并没有改变它们需要存在的事实。您可能避免的是使用QuestionTest
模型,并使用has\u和\u beliens\u many
。但我个人更喜欢你这样做。我发现了一个没有模型的例子。但我做这件事的方式让我很难堪。
<h1>New Test</h1>
<%= form_for @test do |f| %>
<%= f.label :name, "Test Name" %><br>
<%= f.text_field :name, class: "input-lg" %>
<%= f.label :description, "Description" %><br>
<%= f.text_field :description, class: "input-lg" %>
<%= f.label :question_id %><br>
<%= f.select :question_id, Question.all.collect { |p| [ p.content, p.id ] }, class: "input-lg" %>
<%= f.label :category %><br>
<%= f.select :category, [ ["IP Voice Telephony", "ip_voice"], ["IP Video Surveillance", "ip_video_surveillance"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"] ], {prompt: "Select Category"}, class: "input-lg" %>
<%= f.label :type %><br>
<%= f.select :type, [ ["Beginner", "beginner"], ["Intermediate", "intermediate"], ["Advanced", "advanced"] ], {prompt: "Select Type"}, class: "input-lg" %>
<br/><br/><br/>
<%= f.submit "Create Test", class: "btn btn-lg btn-primary" %>
<% end %>
f.fields_for :questions do |question|
question.select(...)
end`