前提・実現したいこと
※cloud9を開発環境としてつかっています。
現在簡単な投稿のアプリをRailsで作っているのですが、
すでに投稿されたものを修正する機能を追加しようとしたときに
編集をすると
undefined method `update' for nil:NilClass
というエラーが出てしまい、編集が完了できません。
発生している問題・エラーメッセージ
undefined method `update' for nil:NilClass
追記:
id:12のデータを投稿後に編集するようにしたいので
コメントいただいたようにデータが入っていないのかな?と見たところ
2.6.0 :004 > Article.find(12) Article Load (0.2ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ? [["id", 12], ["LIMIT", 1]] => #<Article id: 12, title: "testing article", description: "this is a test", created_at: "2019-06-21 19:13:10", updated_at: "2019-06-21 19:13:10"> 2.6.0 :005 >
となっており、データ自体は存在するようです。
development.logのエラーが出ているところのログです。
app/controllers/articles_controller.rb:23:in `update' [1m[36mArticle Load (0.4ms)[0m [1m[34mSELECT "articles".* FROM "articles" LIMIT ?[0m [["LIMIT", 11]] [1m[36mArticle Load (0.2ms)[0m [1m[34mSELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?[0m [["id", 12], ["LIMIT", 1]] Started GET "/articles/12/edit" for 39.111.211.9 at 2019-06-22 05:22:48 +0000 Cannot render console from 39.111.211.9! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 [1m[35m (0.1ms)[0m [1m[34mSELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC[0m ↳ /home/ec2-user/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 Processing by ArticlesController#edit as HTML Parameters: {"id"=>"12"} [1m[36mArticle Load (0.1ms)[0m [1m[34mSELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?[0m [["id", 12], ["LIMIT", 1]] ↳ app/controllers/articles_controller.rb:7 Rendering articles/edit.html.erb within layouts/application Rendered articles/edit.html.erb within layouts/application (4.7ms) Completed 200 OK in 215ms (Views: 200.8ms | ActiveRecord: 0.7ms) Started PATCH "/articles/12" for 39.111.211.9 at 2019-06-22 05:22:56 +0000 Cannot render console from 39.111.211.9! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by ArticlesController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"RvxoB0HoLqxeKxzzVF0q3RnSwQ2Gt6NQGXQ2v3JArvL0SmVMx95vddWBXKBndq0nKbBE7W8lp+M5ryxJIZvWLg==", "article"=>{"title"=>"testing article", "description"=>"this is a test edited"}, "commit"=>"Update Article", "id"=>"12"} [1m[36mArticle Load (0.1ms)[0m [1m[34mSELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?[0m [["id", 12], ["LIMIT", 1]] ↳ app/controllers/articles_controller.rb:22 Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.1ms)
該当のソースコード
github:
https://github.com/yuks0810/alpha-blog
ruby:
1class ArticlesController < ApplicationController 2 def new 3 @article = Article.new 4 end 5 6 def edit 7 @article = Article.find(params[:id]) 8 end 9 10 def create 11 @article = Article.new(article_params) 12 if @article.save 13 flash[:notice] = "Article was successfully created" 14 redirect_to article_path(@article) 15 else 16 render 'new' 17 end 18 end 19 # create action does not need a template as def new does 20 21 def update 22 @artcle = Article.find(params[:id]) 23 if @article.update(article_params) 24 flash[:notice] = "Article was successfully updated" 25 redirect_to article_path(@article) 26 else 27 render "edit" 28 end 29 end 30 31 def show 32 @article = Article.find(params[:id]) 33 end 34 35 private 36 def article_params 37 params.require(:article).permit(:title, :description) 38 end 39 40end 41
undefined methodとなっていますが、def updateで定義していてタイポでもないと思うので
なぜエラーが出るのかわかりません。
どなたかわかる方いらっしゃいましたらご教示お願い致します。
回答2件
あなたの回答
tips
プレビュー