我刚刚回答Submitting multiple forms in 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]'
并尝试过:
= select("registrations[][title]", Registration::TITLE)
返回:
错误的参数数量(2个为3)
看看下面的参数,事件(_id)只有一次然后:标题奇怪开始,任何想法可能是什么问题?
{"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"}
请注意:膳食和成员的编制方式与:标题相同。在此先感谢您的帮助!
修改
因此,通过text_field_tag提交哈希很简单:
= text_field_tag“registrations [] [first_name]”
但问题来自于我的hidden_field_tag和select_tag。
它正在添加错误值,例如:
"title"=>{"registrations"=>"Mr"}
基本上我似乎需要找到一种更好的方法将这些值添加到哈希中。我将继续尝试找到解决方案,并将其发布在此处,除非有人打败我。
答案 0 :(得分:0)
除非我读错了,否则前两个选择呼叫是相同的。你试过= select(:registrations, "title", Registration::TITLE)
吗?如果你看一下api.rubyonrails.org中方法的文档,它会说第一个值是对象,第二个是属性。在参数中,这将是registrations => { :title => "Value" }
。如果您只想要:title => "Value"
,那么您需要select_tag方法。