Rails:NoMethodError错误(未定义的方法`下划线'对于nil:NilClass):'

时间:2014-07-28 12:01:36

标签: ruby-on-rails ruby

我在更新my_member_attributes时遇到错误。 它在终端上说:

NoMethodError (undefined method `underscore' for nil:NilClass):
  app/controllers/clients_controller.rb:345:in `client_params'
  app/controllers/clients_controller.rb:123:in `block in update_children'
  app/controllers/clients_controller.rb:122:in `update_children'

这是我的控制人员:

  def update_children
    binding.pry
    @client = Client.find(params[:id])
    respond_to do |format|
      if @client.update_attributes(client_params)
        flash[:success] = 'Children Information was successfully updated.'
        format.js {
          render :js => "window.location = '#{legal_problem_client_path}'"
        }
      else
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
     end
  end

这是我的client_params

  def client_params
    binding.pry
    if params[:client].nil?
      params_require = params[:type].underscore.to_sym
    else
      params_require = 'client'.to_sym
    end

    params.require(params_require).permit(
      :first_name,
      :middle_name,
      :last_name,
      :company_name,
      :address,
      :mobile_no,
      :telephone_no,
      :email,
      :type,
      :status,
      :point_of_contact,
      :legal_problem,
      :objectives,
      :questions,
      :alternate_email,
      :address_abroad,
      :gender, (and so on)

我在params_require = params[:type].underscore.to_sym中收到错误,因为它表示类型为零,即使我对params有价值:type

这是我的表格:

.box-content
  = form_for(@client, url: update_children_client_path, html: { class: 'form-horizontal', style: 'margin-bottom: 0;' }) do |f|
    = f.hidden_field :is_form_complete, value: false
    - if FamilyMember.name.present?
      = render partial: 'children_list'
    = add_children "Add", f, :family_members, 'children'
    .form-actions.form-actions-padding-sm
      .row
        .col-md-10.col-md-offset-2
          = f.submit "Next", class: "btn btn-lg btn-primary"

这是部分的:

  %tbody
    %tr
      %td
        = f.text_field :name, class: 'form-control'
      %td
        = f.collection_select :children_gender, User::GENDER, :to_s, :titleize, { prompt: true }, { class: 'form-control' }
      %td{:style=> "width: 200px; "}
        .form-group
          .container
            .datetimepicker.input-group.date
              = f.text_field :children_birthdate, class: 'form-control', data: { format: "yyyy-MM-dd" }, placeholder: "Select date", :readonly => 'readonly'
              %span.input-group-addon
                %span.icon-calendar{"data-date-icon" => "icon-calendar", "data-time-icon" => "icon-time"}
      %td
        = f.text_field :children_school, class: 'form-control'
      %td
        = f.text_field :grade_year, class: 'form-control'
        = f.hidden_field :type, value: 'Individual', class: 'form-control'
      %td

继承人的参数:

=> {"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"FnQNiaE5GU4Ivilzi10XuK19GsGYKDirxJd/j7GQ4oI=",
 "individual"=>
  {"is_form_complete"=>"false",
   "family_members_attributes"=>
    {"1406548379040"=>
      {"name"=>"afasfasf",
       "children_gender"=>"",
       "children_birthdate"=>"",
       "children_school"=>"",
       "grade_year"=>"",
       "type"=>"Individual"}}},
 "commit"=>"Next",
 "action"=>"update_children",
 "controller"=>"clients",
 "id"=>"1010"}

1 个答案:

答案 0 :(得分:0)

由于您的参数是嵌套的,因此您必须像{<1}}那样访问

type