前提・実現したいこと
※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
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
flash[:notice] = "Article was successfully created"
redirect_to article_path(@article)
else
render 'new'
end
end
# create action does not need a template as def new does
def update
@artcle = Article.find(params[:id])
if @article.update(article_params)
flash[:notice] = "Article was successfully updated"
redirect_to article_path(@article)
else
render "edit"
end
end
def show
@article = Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title, :description)
end
end
undefined methodとなっていますが、def updateで定義していてタイポでもないと思うので
なぜエラーが出るのかわかりません。
どなたかわかる方いらっしゃいましたらご教示お願い致します。