基本的な質問をさせていただきます。
前提・実現したいこと
ruby on rails で初心者向けのブログ投稿、カテゴリ選択できるwebアプリを作っています。
ローカルホストでhttp://localhost:3000/posts/newで項目を入力し、投稿を作ろうとしましたがエラーが発生しました
発生している問題・エラーメッセージ
/app/views/posts/show.html.erb where line #5 raised:
undefined method `name' for nil:NilClass
該当のソースコード
/app/views/posts/show.html.erb
/app/controllers/posts_controller.rb
class PostsController < ApplicationController
def new @post = Post.new @categories = Category.all end def create post_params = params.require(:post).permit(:category_id, :title, :body) @post = Post.new(post_params) if @post.save redirect_to @post, notice: 'Post was successfully created.' else render :new end end def show @post = Post.find(params[:id]) end def index @posts = Post.all end def edit @post = Post.find(params[:id]) end def update @post = Post.find(params[:id]) post_params = params.require(:post).permit(:category_id, :title, :body) if @post.update(post_params) redirect_to @post, notice: 'Post was successfully updated.' else render :edit end end def destroy @post = Post.find(params[:id]) @post.destroy redirect_to posts_url, notice: 'Post was successfully destroyed.' end
end
試したこと
posts_controllerに問題があるのかと思いましたが、特に見当たりませんでした。
補足情報(言語/FW/ツール等のバージョンなど)
ruby, ruby on rails
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/21 02:37
2016/04/21 04:14 編集