我刚刚回答了在 Rails 中提交多个表单,这导致了另一个问题。在我的表格中,我有以下内容(还有很多):
= hidden_field_tag :event_id, :value => @event.id
.control-group
= label_tag :title
.controls
= select(:registration, "registrations[][title]", Registration::TITLE)
最后一行返回:
"注册"=>[{"职称"=>{"注册"=>"先生"},
与预期相反:
“头衔”=>“先生”
我试过了:
= select(:registration, "registrations[][title]", Registration::TITLE)
返回:
未定义的方法 `registrations[][title]' for #
并尝试过:
= select("registrations[][title]", Registration::TITLE)
返回:
参数数量错误(2 对 3)
看看下面的参数, event(_id) 只存在一次,然后 :title 奇怪开始了,知道问题可能是什么吗?
{"utf8"=>"✓",
"authenticity_token"=>"BQXm5fngW27z/3Wxy9qEzu6D8/g9YQIfBL+mFKVplgE=",
"event_id"=>"7",
"registrations"=>[{"title"=>{"registration"=>"Mr"},
"first_name"=>"Name1",
"last_name"=>"Surname1",
"company_name"=>"Company1",
"designation"=>"Designation1",
"landline"=>"Landline1",
"cell"=>"Cell1",
"email"=>"address1@example.com",
"member"=>{"registration"=>"No"},
"dietary"=>{"registration"=>"None"},
"specify"=>"None"},
{"first_name"=>"Name2",
"last_name"=>"Surname2",
"company_name"=>"Company2",
"designation"=>"Designation2",
"landline"=>"Landline2",
"cell"=>"Cell2",
"email"=>"address2@example.com",
"member"=>{"registration"=>"No"},
"dietary"=>{"registration"=>"None"},
"specify"=>"None",
"title"=>{"registration"=>"Mr"}},
{"first_name"=>"Name3",
"last_name"=>"Surname3",
"company_name"=>"Company3",
"designation"=>"Designation3",
"landline"=>"Landline3",
"cell"=>"Cell3",
"email"=>"address3@example.com",
"member"=>{"registration"=>"No"},
"dietary"=>{"registration"=>"None"},
"specify"=>"None"}],
"commit"=>"Submit registrations"}
请注意:dietary 和:member 的格式与:title 相同。提前感谢你的帮助!
编辑
因此,通过 text_field_tag 提交哈希很简单:
= text_field_tag "注册[][first_name]"
但问题出在我的 hidden_field_tag 和 select_tag 上。
它添加了错误的值,例如:
"title"=>{"registrations"=>"Mr"}
基本上看来我需要找到一种更好的方法将这些值添加到哈希中。我将继续尝试找到解决方案并将其发布在这里,除非有人击败我。