rails 4 form_for with f.select isn't passing correct params, getting `param is missing` error

三世轮回 提交于 2019-12-14 03:21:56

问题


I have a shared partial using select fields and options_for_select that isn't passing the correct params to the controller.

In the users_controller.rb, the view page user_home.html.erb:

def user_home
  if user_signed_in?
    @user = current_user
    @cpe_event = CpeEvent.new
  end
end

In my cpe_events_controller.rb I have a #create action:

def create
  @cpe_event = CpeEvent.new
  @user = current_user     
  @cpe_event = current_user.cpe_events.build(cpe_event_params)
  ...

The data is submitted by a form_for - a shared partial (rendered by a users_controller view) - with a dropdown of several sponsors to select from. And which, I should add, is one of three dropdowns that are chained using 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> 
....      

I suspect the issue is f.select rather than f.text-field - and how one properly submits that data using a form_for.

I am getting this error on the @cpe_event = current_user.cpe_events.build(cpe_event_params) line of the cpe_events_controller.rb:

param is missing or the value is empty: cpe_event

The permitted params code is:

def cpe_event_params
  params.require(:cpe_event).permit(....

If I place raise params.inspect after def create, I get a hash of the params submitted:

{"utf8"=>"✓", "authenticity_token"=>"u1...1g==", "sponsor_name"=>"WI - TEST",
"class_date"=>"2014-01-01", "title"=>"How Account", "commit"=>"Submit",
"controller"=>"cpe_events", "action"=>"create"}

Which is why I'm getting a 400 error, i.e., it doesn't contain

`"cpe_event" => {"sponsor_name"=>"WI - TEST", "class_date"=>"2014-01-01",
 "title"=>"How Account"...`

I've tried submitted the form with a symbol :cpe_event rather than a model object @cpe_event with no luck.

How do I correct the code in order to submit the correct / properly formatted params? Thanks!


回答1:


The question relates to the use of a ruby on rails / rails form (form_for) with select and options_for_select that throws a param is missing error when submitted. The selections are chained using the jQuery Chained Selects Plugin The chaining end-point causes the params error which Clark resolved with these work-arounds:

Here are 2 different options if you want to keep your HTML, and plugin setup as is.

In JQuery - As I say in the comment below, you should add a class to to the specific inputs for cpe_event, ex cpe_event_param and use $('.cpe_event_param') instead of $('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') + ']');
    });
});

or in 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


来源:https://stackoverflow.com/questions/30464733/rails-4-form-for-with-f-select-isnt-passing-correct-params-getting-param-is-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!