前提・実現したいこと
@current_userが存在している状態にしたい。
発生している問題・エラーメッセージ
projects_controller.rbの8行目、user_id: @current_user.idに対し、
NoMethodError in ProjectsController#create undefined method `id' for nil:NilClass
該当のソースコード
application_controller.rb
Ruby
1class ApplicationController < ActionController::Base 2 before_action :set_current_user 3 4 def set_current_user 5 @current_user = User.find_by(id: session[:user_id]) 6 end
projects_controller.rb
Ruby
1def create 2 @project = Project.new( 3 title: params[:title], 4 contents: params[:contents], 5 wanted: params[:wanted], 6 contact_info: params[:contact_info], 7 comments: params[:comments], 8 user_id: @current_user.id 9 ) 10 11 if @project.save 12 flash[:notice] = "プロジェクトを作成しました" 13 redirect_to("/projects/index") 14 else 15 render("projects/new") 16 end 17 end
users_controller.rb
Ruby
1def create 2 @user = User.new( 3 email: params[:email], 4 password: params[:password] 5 ) 6 7 if @user.save 8 session[:user_id] = @user.id 9 flash[:notice] = "ユーザー登録が完了しました" 10 redirect_to("/users/profile/#{@user.id}") 11 else 12 render("signup") 13 end 14 end 15end
ログ
Started POST "/projects/create" for ::1 at 2019-06-30 20:31:15 +0900 ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations" /Users/kyohei/Desktop/match/match_app/app/controllers/application_controller.rb:9: warning: found = in conditional, should be == Processing by ProjectsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"Lcc2pEJVb00UAjmTnTTTJuBrIOh5xoP4EFVkQMoWAnXmHRHJ3D5ICF3KfjCox8yh0NzstWnf2ueGxG29QH4WiA==", "title"=>"s", "contents"=>"s", "wanted"=>"", "contact_info"=>"s", "comments"=>""} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 5], ["LIMIT", 1]] Completed 500 Internal Server Error in 18ms (ActiveRecord: 0.6ms) NoMethodError (undefined method `id' for nil:NilClass): app/controllers/projects_controller.rb:23:in `create' Rendering /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout Rendering /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb Rendered /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (1.9ms) Rendering /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms) Rendering /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb Rendered /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms) Rendered /Users/kyohei/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (72.8ms) ^C- Gracefully stopping, waiting for requests to finish === puma shutdown: 2019-06-30 20:59:10 +0900 ===
試したこと
session[:user_id]も正しく数値が入れられており、DBのusersテーブルにもユーザーは存在しています。しかし@current_userが存在しないとなってしまうのはなぜでしょうか。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/30 11:58
2019/06/30 14:56
2019/06/30 15:48
2019/07/01 08:24 編集