Rails5.1.3でWebアプリケーション制作の勉強中です。たぶんストロングパラメータの所のエラーだと思いますが、
エラーの内容がわからないため自力で解決できないので困っております。
エラーコードは以下になります。
ActionController::ParameterMissing in Customer::AccountsController#update param is missing or the value is empty: home_address Extracted source (around line #65): 63 64 def phone_params(record_name) 65 @params.require(record_name).permit(phones: [ :number, :primary ]) 66 end 67end Rails.root: /home/vagrant/chibi/chibi Application Trace | Framework Trace | Full Trace app/forms/customer/account_form.rb:65:in `phone_params' app/forms/customer/account_form.rb:38:in `assign_attributes' app/controllers/customer/accounts_controller.rb:24:in `update'
コードは以下になります。
app/forms/customer/account_form.rb class Customer::AccountForm include ActiveModel::Model attr_accessor :customer, :inputs_home_address delegate :persisted?, :valid?, :save, to: :customer def initialize(customer) @customer = customer (2 - @customer.personal_phones.size).times do @customer.personal_phones.build end self.inputs_home_address = @customer.home_address.present? @customer.build_home_address unless @customer.home_address (2 - @customer.home_address.phones.size).times do @customer.home_address.phones.build end end def assign_attributes(params = {}) @params = params self.inputs_home_address = params[:inputs_home_address].in? [ '1', 'true' ] customer.assign_attributes(customer_params) phones = phone_params(:customer).fetch(:phones) customer.personal_phones.size.times do |index| attributes = phones[index.to_s] if attributes && attributes[:number].present? customer.personal_phones[index].assign_attributes(attributes) else customer.personal_phones[index].mark_for_destruction end end if inputs_home_address customer.home_address.assign_attributes(home_address_params) phones = phone_params(:home_address).fetch(:phones) 38行目 customer.home_address.phones.size.times do |index| attributes = phones[index.to_s] if attributes && attributes[:number].present? customer.home_address.phones[index].assign_attributes(attributes) else customer.home_address.phones[index].mark_for_destruction end end else customer.home_address.mark_for_destruction end end private def customer_params @params.require(:customer).permit( :family_name, :given_name, :family_name_kana, :given_name_kana, :birthday, :gender, :nickname, :sponsor1 ) end def home_address_params @params.permit(home_address: [:home_address, :postal_code, :prefecture, :city, :address1, :address2]) end def phone_params(record_name) @params.require(record_name).permit(phones: [ :number, :primary ]) ここでエラー発生 65行目 end end
app/controllers/customer/accounts_controller.rb class Customer::AccountsController < Customer::Base def show @customer = current_customer end def edit @customer_form = Customer::AccountForm.new(current_customer) end # PATCH def confirm @customer_form = Customer::AccountForm.new(current_customer) @customer_form.assign_attributes(params[:form]) if @customer_form.valid? render action: 'confirm' else flash.now.alert = '入力に誤りがあります。' render action: 'edit' end end def update @customer_form = Customer::AccountForm.new(current_customer) @customer_form.assign_attributes(params[:form]) 24行目 if params[:commit] if @customer_form.save flash.notice = 'アカウント情報を更新しました。' redirect_to :customer_account else flash.now.alert = '入力に誤りがあります。' render action: 'edit' end else render action: 'edit' end end private def customer_params @params.require(:customer).permit( :family_name, :given_name, :family_name_kana, :given_name_kana, :birthday, :gender, :nickname, :sponsor1 ) end def home_address_params @params.require(:home_address).permit(:home_address, :postal_code, :prefecture, :city, :address1, :address2) end def phone_params(record_name) @params.require(record_name).permit(phones: [ :number, :primary ]) end end
自力で解決できないので、どなたか助けていただけないでしょうか?宜しくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。