Rails5.1.3でWebアプリケーション制作の勉強をしています。
保存しようとするとcreateアクションでif @customer_form.saveができなく
「入力に誤りがあります。」と表示され@customer_form.saveができていないのです。
フォームに入力したデータを保存したいのですが、保存ができなくて困っております。
$rails c でcreateアクションを1行ずつ実行したら
NoMethodError: private method `require' called for #<Hash:0x0055a8bc285748>
のようなエラーが出ていました。関連するコードは以下になります。
customers_controller.rb class Visitor::CustomersController < Visitor::Base def new @customer_form = Visitor::CustomerForm.new end def create @customer_form = Visitor::CustomerForm.new @customer_form.assign_attributes(params[:form]) if @customer_form.save flash.notice = '顧客アカウントを新規登録しました。' redirect_to :new_visitor_historie else flash.now.alert = '入力に誤りがあります。' render action: 'new' end end private def customer_params params.permit(customer: [:email, :password, :family_name, :given_name, :family_name_kana, :given_name_kana]) end end
visitor/customer_form.rb class Visitor::CustomerForm include ActiveModel::Model attr_accessor :customer, :inputs_home_address delegate :persisted?, :save, to: :customer def initialize(customer = nil) @customer = customer @customer ||= Customer.new (2 - @customer.personal_phones.size).times do @customer.personal_phones.build end self.inputs_home_address = @customer.home_address.blank? @customer.build_home_address unless @customer.home_address end def assign_attributes(params = {}) @params = params self.inputs_home_address = params[:inputs_home_address] == '1' 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) else customer.home_address.mark_for_destruction end end private def phone_params(record_name) @params.require(record_name).permit(phones: [ :number, :primary ]) end def customer_params @params.require(:customer).permit(:email, :password, :family_name, :given_name, :family_name_kana, :given_name_kana) ------ここでエラー発生! end def home_address_params @params.require(:home_address).permit(:postal_code, :prefecture, :city, :address1, :address2 ) end end
$ rails c にて実行し表示された内容 [vagrant@localhost edamame]$ rails c Loading development environment (Rails 5.1.6) irb(main):001:0> @customer_form = Visitor::CustomerForm.new (0.5ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 => #<Visitor::CustomerForm:0x0055a8bc3982c0 @customer=#<Customer id: nil, email: nil, email_for_index: nil, family_name: nil, given_name: nil, family_name_kana: nil, given_name_kana: nil, hashed_password: nil, created_at: nil, updated_at: nil>, @inputs_home_address=true> irb(main):002:0> @customer_form.assign_attributes(email: "sato.ichiro@example.jp", password: "password", family_name: "佐藤", given_name: "一郎", family_name_kana: "サトウ", given_name_kana: "イチロウ") NoMethodError: private method `require' called for #<Hash:0x0055a8bc285748> from app/forms/visitor/customer_form.rb:46:in `customer_params' from app/forms/visitor/customer_form.rb:21:in `assign_attributes'
どなたか解決方法を教えていただけないでしょうか?宜しくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/15 02:40