rails 4 form_for with f.select 没有传递正确的参数,出现“参数丢失”错误
rails 4 form_for with f.select isn't passing correct params, getting `param is missing` error
我有一个使用 select
字段和 options_for_select
的共享部分,它没有将正确的参数传递给控制器。
在users_controller.rb,查看页面user_home.html.erb:
def user_home
if user_signed_in?
@user = current_user
@cpe_event = CpeEvent.new
end
end
在我的 cpe_events_controller.rb 中,我有一个 #create 操作:
def create
@cpe_event = CpeEvent.new
@user = current_user
@cpe_event = current_user.cpe_events.build(cpe_event_params)
...
数据由 form_for 提交 - 共享部分(由 users_controller 视图呈现) - 有几个赞助商的下拉列表来自 select。我要补充的是,它是使用 jQuery:
链接的三个下拉菜单之一
<%= form_for @cpe_event, url: cpe_events_path, method: :post,
html: { multipart: true }, class: 'form-inline form_for form_for_content' do |f| %>
<div>
<div class="col-sm-3 col-md-3" style="float: left;">
<% f.label :sponsor_name %>
<%= f.select :sponsor_name, options_for_select(@sponsor_names), {},
{ class: "sponsor_name form-control btn-default", name: "sponsor_name"} %>
</div>
....
我怀疑问题出在 f.select
而不是 f.text-field
- 以及如何使用 form_for 正确提交该数据。
我在 cpe_events_controller.rb
的 @cpe_event = current_user.cpe_events.build(cpe_event_params)
行收到此错误:
param is missing or the value is empty: cpe_event
允许的参数代码是:
def cpe_event_params
params.require(:cpe_event).permit(....
如果我将 raise params.inspect
放在 def create
之后,我会得到提交的参数的哈希值:
{"utf8"=>"✓", "authenticity_token"=>"u1...1g==", "sponsor_name"=>"WI - TEST",
"class_date"=>"2014-01-01", "title"=>"How Account", "commit"=>"Submit",
"controller"=>"cpe_events", "action"=>"create"}
这就是我收到 400 错误的原因,即它不包含
`"cpe_event" => {"sponsor_name"=>"WI - TEST", "class_date"=>"2014-01-01",
"title"=>"How Account"...`
我尝试提交带有符号 :cpe_event
而不是模型对象 @cpe_event
的表单,但没有成功。
如何更正代码以提交正确/格式正确的参数?谢谢!
问题涉及 ruby 在 rails / rails 表单 (form_for
) 上的使用 select
和 options_for_select
提交时抛出 param is missing error
。使用 jQuery Chained Selects Plugin 链接选择链接端点导致参数错误,Clark 使用这些解决方法解决了该错误:
如果您想保持 HTML 和插件设置不变,这里有 2 个不同的选项。
In JQuery - 正如我在下面的评论中所说,您应该将 class 添加到 cpe_event 的特定输入,例如 cpe_event_param
并使用 $('.cpe_event_param')
而不是 $('input')
$('form').submit(function() {
$('input').each(function() { //should probably use a class for this so you only do it to specific inputs
$(this).attr('name', 'cpe_event[' + $(this).attr('name') + ']');
});
});
或 rails -
def cpe_event_params
cpe_event = Hash.new
params.each do |k,v|
cpe_event[k] = v
end
params[:cpe_event] = cpe_event
#everything else you had
我有一个使用 select
字段和 options_for_select
的共享部分,它没有将正确的参数传递给控制器。
在users_controller.rb,查看页面user_home.html.erb:
def user_home
if user_signed_in?
@user = current_user
@cpe_event = CpeEvent.new
end
end
在我的 cpe_events_controller.rb 中,我有一个 #create 操作:
def create
@cpe_event = CpeEvent.new
@user = current_user
@cpe_event = current_user.cpe_events.build(cpe_event_params)
...
数据由 form_for 提交 - 共享部分(由 users_controller 视图呈现) - 有几个赞助商的下拉列表来自 select。我要补充的是,它是使用 jQuery:
链接的三个下拉菜单之一<%= form_for @cpe_event, url: cpe_events_path, method: :post,
html: { multipart: true }, class: 'form-inline form_for form_for_content' do |f| %>
<div>
<div class="col-sm-3 col-md-3" style="float: left;">
<% f.label :sponsor_name %>
<%= f.select :sponsor_name, options_for_select(@sponsor_names), {},
{ class: "sponsor_name form-control btn-default", name: "sponsor_name"} %>
</div>
....
我怀疑问题出在 f.select
而不是 f.text-field
- 以及如何使用 form_for 正确提交该数据。
我在 cpe_events_controller.rb
的 @cpe_event = current_user.cpe_events.build(cpe_event_params)
行收到此错误:
param is missing or the value is empty: cpe_event
允许的参数代码是:
def cpe_event_params
params.require(:cpe_event).permit(....
如果我将 raise params.inspect
放在 def create
之后,我会得到提交的参数的哈希值:
{"utf8"=>"✓", "authenticity_token"=>"u1...1g==", "sponsor_name"=>"WI - TEST",
"class_date"=>"2014-01-01", "title"=>"How Account", "commit"=>"Submit",
"controller"=>"cpe_events", "action"=>"create"}
这就是我收到 400 错误的原因,即它不包含
`"cpe_event" => {"sponsor_name"=>"WI - TEST", "class_date"=>"2014-01-01",
"title"=>"How Account"...`
我尝试提交带有符号 :cpe_event
而不是模型对象 @cpe_event
的表单,但没有成功。
如何更正代码以提交正确/格式正确的参数?谢谢!
问题涉及 ruby 在 rails / rails 表单 (form_for
) 上的使用 select
和 options_for_select
提交时抛出 param is missing error
。使用 jQuery Chained Selects Plugin 链接选择链接端点导致参数错误,Clark 使用这些解决方法解决了该错误:
如果您想保持 HTML 和插件设置不变,这里有 2 个不同的选项。
In JQuery - 正如我在下面的评论中所说,您应该将 class 添加到 cpe_event 的特定输入,例如 cpe_event_param
并使用 $('.cpe_event_param')
而不是 $('input')
$('form').submit(function() {
$('input').each(function() { //should probably use a class for this so you only do it to specific inputs
$(this).attr('name', 'cpe_event[' + $(this).attr('name') + ']');
});
});
或 rails -
def cpe_event_params
cpe_event = Hash.new
params.each do |k,v|
cpe_event[k] = v
end
params[:cpe_event] = cpe_event
#everything else you had