LoginSignup
8
8

More than 5 years have passed since last update.

NoMethodError in Posts#show の解決方法について(自分用メモ)

Posted at

現在、チュートリアルRubyOnRailsで投稿アプリを作ろう②(画面作成編)に沿ってRuby on railsの学習を進めている初心者です。

NoMethodError in Posts#showというエラー

各ユーザーの投稿の詳細を表示しようとしたところ、こんなエラーが出てしまいました・・・

スクリーンショット (70).png

ちなみに、作業中の各ディレクトリは以下のとおりです。

post_controller.rb
# Applicationcontrollerクラスを継承することで、クラスがコントローラと認識される
class PostsController < ApplicationController
    def index
        # 投稿データを全て取得、またインスタンス変数なのでViewで参照可能
        @posts = Post.all
    end

    #ルーティングの変更後に追加
    def new
        #Postモデルのオブジェクトを作成
        # @postはインスタンス変数でviewで参照可能
        @post = Post.new
    end

    def create
    #データはparamsという変数に渡されている
    #createはPostモデルのクラスメソッド
    Post.create(post_params)
    end


    private

    #paramsから欲しいデータのみ抽出
    def post_params
        params.require(:post).permit(:name, :title, :content)
    end

    # findメソッドで、idにひもづくPOSTオブジェクトを取得する
    def show
     @post = Post.find(params[:id])
    end

end
show.html.erb
<div class="d-flex align-items-center mt-4 mb-4">
    <div class="ml-auto posts_button">
        <a class="btn btn-outline-info" href="/posts" role="button">投稿一覧</a>
        <a class="btn btn-outline-info" href="/posts" role="button">編集</a>
    </div>
</div>

<div class="card">
  <div class="card-header bg-info text-white">
      <h4><%= @post.title %></h4>
  </div>
  <div class="card-body">
      <p class="card-text"><%= simple_format(@post.content) %></p>
  </div>
  <div class="card-footer">
      <p class="text-right font-weight-bold mr-10"><%= @post.name %></p>
  </div>
</div>
routes.rb
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

    # root -> ルートパス(localhost:3000)でアクセスした場合に割り当てられる設定で、Controllerファイルに記述するindexメソッドを実行するように定義
    get 'posts', to: 'posts#index'

    #投稿ページを表示
    get 'posts/new', to: 'posts#new'

    #投稿データの作成
    post 'posts', to: 'posts#create'

    # 投稿ページ表示(追加)
    get 'posts/:id', to: 'posts#show'

end

db/migrate

$ rails generate model post name:string title:string content:textを実行すると、dbディレクトリ配下にマイグレーションファイルが作成される


class CreatePosts < ActiveRecord::Migration[5.0]
  def change
    create_table :posts do |t|
      t.string :name
      t.string :title
      t.text :content

      t.timestamps
    end
  end
end

privateより先にメソッドを記述することで解決

解決方法についてこちらのサイトを参考に↓
NoMethodError in Posts#show ;

All methods defined after private are accessible only internally. Move the show method above private. And make sure you have a file called app/views/posts/show.html.erb and not .rb

メソッドはprivateの前におかなきゃだよー!
とのことでしたので、以下のとおりprivateの前にshowメソッドを移動させました。

posts_contoroller.rb




# findメソッドで、idにひもづくPOSTオブジェクトを取得する
    def show
     @post = Post.find(params[:id])
    end


    private

    #paramsから欲しいデータのみ抽出
    def post_params
        params.require(:post).permit(:name, :title, :content)
    end

end

投稿詳細ページが無事表示されました!

スクリーンショット (72).png

何か間違っていることなどありましたらご連絡ください!

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8