我是Rails的新手,我一直在玩,直到我与争执。我已经阅读了很多关于这个网站的问题,但我仍然没有解决我的问题。我相信这是我错过的东西。Rails的3.1 - 试图让check_box_tag与查找表一起工作
我有一个reportdetail
表和一个buginfo
表。 buginfo
表代表我想在某些程序中找到的错误的只读表。 reportdetail
表将包含报告,其中将包含用户想要查找的错误列表(来自buginfo表)。我可以在报表详细信息表中包含许多报表。
class Reportdetail < ActiveRecord::Base
has_many :buginfos
end
class Buginfo < ActiveRecord::Base
belongs_to :reportdetail
end
这是我的模式。这很简单:
ActiveRecord::Schema.define(:version => 20111221162309) do
create_table "buginfos", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "reportdetails", :force => true do |t|
t.string "name"
t.integer "buginfo_id"
t.boolean "bug_detected"
t.datetime "created_at"
t.datetime "updated_at"
end
end
最后我对reportdetail
部分_form.html.erb:
<%= form_for(@reportdetail) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<table>
<% Buginfo.find(:all).each do |buginfo| %>
<tr>
<td> <%= check_box_tag("[buginfo_id][]",1,buginfo.id) %> </td>
<td> <%= buginfo.name %> </td>
</tr>
<% end %>
</table>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
的形式正确地显示旁边的每一个错误复选框,我甚至可以点击复选框,但是数据永远不会被保存。我知道的第一个参数需要是唯一的,我很确定这是正确的。我知道第二个参数是复选框的值,但这是什么意思?我假设这意味着这个bug是否被选中的当前值,但由于我还没有选择任何东西,我不知道该怎么做。根据我读的文档,第三个参数应该是“复选框是否被选中?”但是,再一次,这是什么关系? - 当前值或...
如果用户正在创建新的reportdetail
,并假设buginfos数据库中有5个错误,那么我会假设5个复选框显示在/ reportdetail/new上。这发生正确。如果我为reportdetail选择3个bug(选择复选框),我会假设在数据库中会创建三个记录来表示报告要包含的每个错误。这完全没有发生。我只得到一条记录,buginfo_id和bug_detected都是零。
任何指导将不胜感激。感谢你给与我的帮助。
你真正需要的是'fields_for' http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for – Damien 2011-12-21 18:02:22