I got an error while I'm updating my 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
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
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]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.