質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

2364閲覧

Deviseでのundefined method `[]=' for nil:NilClassエラー

kenkun777

総合スコア10

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2018/10/03 03:50

前提・実現したいこと

Deviseを実装して新規登録をしても
下記のエラーでHTMLが表示されない。

発生している問題・エラーメッセージ

NoMethodError in Users::RegistrationsController#new undefined method `[]=' for nil:NilClass Extracted source (around line #39):

該当のソースコード

class Users::RegistrationsController < Devise::RegistrationsController def create build_resource(sign_up_params) resource.save yield resource if block_given? if resource.persisted? if resource.active_for_authentication? set_flash_message! :notice, :signed_up sign_up(resource_name, resource) create_profile resource respond_with resource, location: after_sign_up_path_for(resource) else set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}" expire_data_after_sign_in! respond_with resource, location: after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource set_minimum_password_length flash[:messages] = resource.errors.full_messages redirect_to new_user_registration_path, alert: I18n.t('.errors.messages.not_completed', count: resource.errors.size) end end def edit @mode = params.fetch(:mode, "email") super end def update @mode = params[:mode] super end protected def build_resource(hash=nil) hash[:uid] = User.create_unique_string super end def update_needs_confirmation?(resource, previous) resource.respond_to?(:pending_reconfirmation?) && resource.pending_reconfirmation? && resource.email != resource.unconfirmed_email end private def create_profile(resource) profile = Profile.create(user_id: resource.id) ProfileImage.create(user_id: resource.id, profile_id: profile.id, image: '', caption: '') end end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

下記部分でhashがnilの場合に起こります。

ruby

1def build_resource(hash=nil) 2 hash[:uid] = User.create_unique_string 3 super 4end

デフォルト値をnilにしてるので、おそらく引数無しで呼び出してる箇所があるのではないでしょうか?

投稿2018/10/03 03:58

dice142

総合スコア5158

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kenkun777

2018/10/03 04:03

なるほど!!ありがとうございます。ん〜引数無しで呼び出している箇所…どこだ…。
退会済みユーザー

退会済みユーザー

2018/10/03 04:13

もしくはbuild_resource(sign_up_params)のsign_up_paramsがnilの場合があるとか...ですかね。
kenkun777

2018/10/03 04:54

確かにこれ怪しいですね…。
dice142

2018/10/03 07:08

新規登録だからそっちの方が可能性高そうですね。
kenkun777

2018/10/03 08:07

あれから色々いじってみたものの、今だ解決せずです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問