空投稿のページを開くことが出来ません。
contentがnilであるため、
エラー
NoMethodError in Posts#show
undefined method `content' for nil:NilClass
が出てきてしまいます。
show.html.erb
<div class="main posts-show"> <div class="container"> <div class="posts-show-item"> <p> <%= @post.content %> </p> <div class="post-time"> <%= @post.created_at.strftime("%Y/%m/%d %H:%M") %> </div> <div class="post-menus"> <%= link_to("編集", "/posts/#{@post.id}/edit") %> <%= link_to("削除", "/posts/#{@post.id}/destroy", {method:"post"}) %> </div> </div> </div> </div>
posts_controller.rb
class PostsController < ApplicationController def index @posts = Post.all.order(created_at: :desc) end def show @post = Post.find_by(id: params[:id]) end def new end def create @post = Post.new(content: params[:content]) @post.save redirect_to("/posts/index") end def edit @post = Post.find_by(id: params[:id]) end def update @post = Post.find_by(id: params[:id]) @post.content = params[:content] if @post.save redirect_to("/posts/index") else render("posts/edit") end end def destroy @post = Post.find_by(id: params[:id]) @post.destroy redirect_to("/posts/index") end end
よろしくお願いいたします。
これだけでは判断できかねますのでcontrollerクラスの提示をお願いします。
回答1件
あなたの回答
tips
プレビュー