Rails5.1.3でWebアプリケーション制作の勉強をしています。
データを更新しようとしておりますが以下のエラ-が発生し解決できずに困っております。エラーは以下のようになります。
エラーの内容 ActiveModel::UnknownAttributeError in Customer::CustomersController#update unknown attribute 'customer' for Customer. Extracted source (around line #23): 21 self.inputs_history = params[:inputs_history] == '1' 22 23 customer.assign_attributes(customer_params) ----------ここでエラーが発生しているようです。 24 25 phones = phone_params(:customer).fetch(:phones) 26 customer.personal_phones.size.times do |index| Rails.root: /home/vagrant/fujiyoshi/edamame Application Trace | Framework Trace | Full Trace app/forms/customer/customer_form.rb:23:in `assign_attributes' app/controllers/customer/customers_controller.rb:30:in `update' Request Parameters: {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"yf5qIHIKjCyqxg0bTnxcuPUlLuFZlT11/mnzsxHGL1SB19TRKB1J98vZlIF5ScBWOHk8zyPPBMt7/KHJ4DYhwQ==", "form"=> {"customer"=> {"email"=>"sato.ichiro@example.jp", "family_name"=>"佐藤", "given_name"=>"一郎", "family_name_kana"=>"サトウ", "given_name_kana"=>"イチロウ", "phones"=>{"0"=>{"number"=>"0184362632", "primary"=>"0"}, "1"=>{"number"=>"07011456699", "primary"=>"0"}}}, "inputs_home_address"=>"1", "home_address"=>{"postal_code"=>"0180436", "prefecture"=>"宮崎県", "city"=>"一番市", "address1"=>"49", "address2"=>"レイルズハイツ301号室"}}, "commit"=>"更新", "host"=>"fujiyoshi.nouen.com", "id"=>"1"}
customers_controller.rb def edit @customer_form = Customer::CustomerForm.new(Customer.find(params[:id])) end def update @customer_form = Customer::CustomerForm.new(Customer.find(params[:id])) @customer_form.assign_attributes(params[:form]) if @customer_form.save flash.notice = '顧客情報を更新しました。' redirect_to action: 'index' else flash.now.alert = '入力に誤りがあります。' render action: 'edit' end end private def customer_params params.permit(customer: [:email, :password, :family_name, :given_name, :family_name_kana, :given_name_kana, :start_date, :end_date]) end
customer_form.rb def assign_attributes(params = {}) @params = params self.inputs_home_address = params[:inputs_home_address] == '1' self.inputs_history = params[:inputs_history] == '1' customer.assign_attributes(customer_params) phones = phone_params(:customer).fetch(:phones) customer.personal_phones.size.times do |index|
models/customer.rb class Customer < ActiveRecord::Base include EmailHolder include PersonalNameHolder include PasswordHolder has_many :addresses, dependent: :destroy has_one :home_address, autosave: true has_many :phones, dependent: :destroy has_many :personal_phones, -> { where(address_id: nil).order(:id) }, class_name: 'Phone', autosave: true has_many :histories, dependent: :destroy, autosave: true end
20180711065437_create_customers.rb class CreateCustomers < ActiveRecord::Migration[5.1] def change create_table :customers do |t| t.string :email, null: false #メールアドレス t.string :email_for_index, null: false t.string :family_name, null: false t.string :given_name, null: false t.string :family_name_kana, null: false t.string :given_name_kana, null: false t.string :hashed_password t.timestamps end add_index :customers, :email_for_index, unique: true add_index :customers, [ :family_name_kana, :given_name_kana ] end end
このようにしています。
自分なりに調べてみたらCustomerテーブルにはcustomerカラムはありません。というようなエラーのようでしたが、もともとcustomerカラムははじめから作っておりません。
どなたか解決方法を教えていただけないでしょうか?宜しくお願いします。
追記
指摘していただいた所を修正したところ
Unpermitted parameter: :phones
Unpermitted parameters: :email, :family_name, :given_name, :family_name_kana, :given_name_kana
となりupdateできませんでした。表示されたのは以下になります。
Started PATCH "/customers/1" for 10.0.2.2 at 2018-09-07 09:38:31 +0900 Processing by Customer::CustomersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"TuF5A8ETAD98R1Qlvesf4ZiH+RsHWBLCcwLVOfuOKF7AwsQKO11fmSVbJCiaxmUmN3QLNqMdMdqbuE7Trmk5Zg==", "form"=>{"customer"=>{"email"=>"sato.ichiro@example.jp", "family_name"=>"佐藤あ", "given_name"=>"一郎", "family_name_kana"=>"サトウ", "given_name_kana"=>"イチロウ", "phones"=>{"0"=>{"number"=>"0184-36-2632", "primary"=>"0"}, "1"=>{"number"=>"070-1145-6699", "primary"=>"0"}}}, "inputs_home_address"=>"1", "home_address"=>{"postal_code"=>"9204279", "prefecture"=>"長崎県", "city"=>"[\"赤巻市\"]", "address1"=>"開発1-2-3", "address2"=>"レイルズハイツ301号室"}}, "commit"=>"更新", "host"=>"fujiyoshi.nouen.com", "id"=>"1"} Customer Load (0.5ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 Customer Load (0.4ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 (0.5ms) SELECT COUNT(*) FROM `phones` WHERE `phones`.`customer_id` = 1 AND `phones`.`address_id` IS NULL HomeAddress Load (0.5ms) SELECT `addresses`.* FROM `addresses` WHERE `addresses`.`type` IN ('HomeAddress') AND `addresses`.`customer_id` = 1 LIMIT 1 Unpermitted parameter: :phones Unpermitted parameters: :email, :family_name, :given_name, :family_name_kana, :given_name_kana (0.2ms) BEGIN Customer Exists (0.5ms) SELECT 1 AS one FROM `customers` WHERE `customers`.`email_for_index` = BINARY 'sato.ichiro@example.jp' AND (`customers`.`id` != 1) LIMIT 1 CACHE Customer Load (0.0ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 [["id", 1], ["LIMIT", 1]] CACHE Customer Load (0.0ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 [["id", 1], ["LIMIT", 1]] CACHE Customer Load (0.0ms) SELECT `customers`.* FROM `customers` WHERE `customers`.`id` = 1 LIMIT 1 [["id", 1], ["LIMIT", 1]] (0.3ms) ROLLBACK Rendering customer/customers/edit.html.erb within layouts/customer Rendered customer/customers/_phone_fields.html.erb (2.7ms) Rendered customer/customers/_phone_fields.html.erb (2.1ms) Rendered customer/customers/_customer_fields.html.erb (127.0ms) Rendered customer/customers/_home_address_fields.html.erb (22.5ms) Rendered customer/customers/_form.html.erb (156.8ms) Rendered customer/customers/edit.html.erb within layouts/customer (161.1ms) Rendered customer/shared/_header.html.erb (2.8ms) Rendered shared/_footer.html.erb (0.7ms) Completed 200 OK in 1274ms (Views: 1159.2ms | ActiveRecord: 3.0ms)
全くのお手上げですので再度ご指摘いただけないでしょうか?お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/07 00:46