简体   繁体   English

Rails:“ NoMethodError(对于nil:NilClass的未定义方法'underscore')上的错误:”

[英]Rails: Error on 'NoMethodError (undefined method `underscore' for nil:NilClass):'

I got an error while I'm updating my family_member_attributes. 更新我的family_member_attributes时出现错误。 It say's on the terminal: 它说在终端上:

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'

Here's my controller: 这是我的控制器:

  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

and this is my client_params 这是我的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)

I got an error in in params_require = params[:type].underscore.to_sym because it says that type is nil even I have value for params :type 我在params_require = params[:type].underscore.to_symparams_require = params[:type].underscore.to_sym错误,因为它说类型为nil,即使我对params有价值:type

this is my form: 这是我的表格:

.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"

And this is the partial: 这是部分的:

  %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

heres's my parameters: 这是我的参数:

=> {"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"}

由于您的参数是嵌套的,因此您必须像这样访问type

params[:individual][:family_members_attributes]['1406548379040'][:type]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails控制台错误:nil的未定义方法`each&#39;:NilClass(NoMethodError) - Rails console error: undefined method `each' for nil:NilClass (NoMethodError) Rails - 测试夹具错误NoMethodError:nil的未定义方法`type&#39;:NilClass - Rails - Testing fixtures error NoMethodError: undefined method `type' for nil:NilClass Rails:错误“ NoMethodError:nil:NilClass的未定义方法&#39;&lt;&#39;”,缺少一些gem吗? - Rails: Error “NoMethodError: undefined method `<' for nil:NilClass”, missing some gem? Rails4:NoMethodError /未定义的方法&#39;*&#39;为nil:NilClass - Rails4: NoMethodError / undefined method `*' for nil:NilClass Rails-NoMethodError-未定义的方法“名称”,为nil:NilClass - Rails - NoMethodError - undefined method `name' for nil:NilClass Rails 6-NoMethodError(nil:NilClass的未定义方法“ titleize”) - Rails 6 - NoMethodError (undefined method `titleize' for nil:NilClass) Rails 4:NoMethodError:nil的未定义方法`each&#39;:NilClass - Rails 4: NoMethodError: undefined method `each' for nil:NilClass Rails DateTime-NoMethodError(nil:NilClass的未定义方法“ []”) - Rails DateTime - NoMethodError (undefined method `[]' for nil:NilClass) Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass Ruby on Rails NoMethodError:nil:NilClass的未定义方法“ []” - Ruby on Rails NoMethodError: undefined method `[]' for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM