#解決したいこと
新規会員登録をウィザード形式で作成しています。会員情報(user)→住所(sending_destination)の順にページを移動して入力します。
会員登録にはdeviseを使用。ページ移動時には入力情報をsessionに保持します。
会員情報を入力後、住所入力ページに移動する際に以下のエラーが発生しています。
new_sending_destination.html.haml
.main-form = form_for @sending_destination do |f| = devise_error_messages! %h2.main-form__title お届け先住所入力 .main-form__wrapper .main-contents .contents-field = f.label :お名前(全角) %span.contents-field__require 必須 = f.text_field :family_name, class:"contents-field__input half", placeholder:"例)山田" = f.text_field :first_name, class:"contents-field__input half", placeholder:"例)太郎" .contents-field = f.label :お名前カナ(全角) %span.contents-field__require 必須 = f.text_field :family_name_kana, class:"contents-field__input half", placeholder:"例)ヤマダ" = f.text_field :first_name_kana, class:"contents-field__input half", placeholder:"例)タロウ" .contents-field = f.label :郵便番号 %span.contents-field__require 必須 = f.number_field :post_code, class:"contents-field__input", placeholder:"例)555-7777" .contents-field = f.label :都道府県 %span.contents-field__require 必須 = f.text_field :prefecture_code, class:"contents-field__input", placeholder:"例)555-7777" .contents-field = f.label :市区町村 %span.contents-field__require 必須 = f.text_field :city, class:"contents-field__input", placeholder:"例)渋谷区" .contents-field = f.label :番地 %span.contents-field__require 必須 = f.text_field :house_number, class:"contents-field__input", placeholder:"例)神南1丁目18−2" .contents-field = f.label :番地 %span.contents-field__require 任意 = f.text_field :building_name, class:"contents-field__input", placeholder:"例)フレーム神南坂 4階" .contents-field = f.label :電話番号 %span.contents-field__require 任意 = f.number_field :phone_number, class:"contents-field__input", placeholder:"例)22233334444"
sending_destination.rb
class SendingDestination < ApplicationRecord belongs_to :user, optional: true validates :post_code, :prefecture_code, :city, :house_number, :building_name, :phone_number ,presence: true end
registration_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController before_action :configure_sign_up_params, only: [:create] def new @user = User.new end def create @user = User.new(sign_up_params) unless @user.valid? flash.now[:alert] = @user.errors.full_messages render :new and return end session["devise.regist_data"] = {user: @user.attributes} session["devise.regist_data"][:user]["password"] = params[:user][:password] @sending_destination = @user.sending_destination render :new_sending_destination end def create_sending_destination @user = User.new(session["devise.regist_data"]["user"]) @sending_destination = SendingDestination.new(sending_destination_params) unless @sending_destination.valid? flash.now[:alert] = @sending_destination.errors.full_messages render :new_sending_destination and return end @user.build_sending_destination(@sending_destination.attributes) @user.save sign_in(:user, @user) end protected def configure_sign_up_params devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) end def sending_destination_params params.require(:sending_destination).permit(:post_code, :prefecture_code, :city, :house_number, :building_name, :phone_number) end
maigation(sending_desitnation)
class CreateSendingDestinations < ActiveRecord::Migration[5.0] def change create_table :sending_destinations do |t| t.string :destination_family_name, null: false t.string :destination_first_name, null: false t.string :destination_first_name_kana, null: false t.string :destination_family_name_kana, null: false t.integer :post_code, null: false t.integer :prefecture_code, null: false t.string :city, null:false t.string :house_number, null: false t.string :building_name t.integer :phone_number, unique: true t.references :user, foreign_key: true end end end
よろしくお願いいたします。
現時点での状況をお聞きしたいのでお答えください。
1. エラーメッセージの意味がわからない?
2. エラーメッセージの意味は理解しているが、それがどこでおきているのかわからない?
3. エラーメッセージも原因となっている箇所もわかっているが、具体的な修正アプローチがわからない?
registration_controller.rbのcreateで以下のようにログ仕込んだら何か出力されますか?
>@sending_destination = @user.sending_destination
>logger.debug @user.sending_destination
Mugheartさん
状況としては1番です。
質問する際、もう少し詳細に描くように気をつけます。ご指摘ありがとうございます!
「form_for の第一引数に nil を渡すことはできません」という旨のメッセージです。
もう少し自分で何かしら試してみてください。
あなたの回答
tips
プレビュー