質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

3358閲覧

NameError in PostsController#createのエラーを解消したい

tetsuya7724

総合スコア67

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/04/20 05:03

前提・実現したいこと

rails 6で作成しているのですが、このようなエラーが発生しています。

発生している問題・エラーメッセージ

NameError in PostsController#create undefined local variable or method `post_params' for #<PostsController:0x00007ff65730ff48> Did you mean? post_path

イメージ説明

該当のソースコード

Ruby

1#controller 2 3class PostsController < ApplicationController 4 before_action :authenticate_user 5 before_action :ensure_correct_user, {only: [:edit, :update, :destroy]} 6 7 # GET /posts 8 # GET /posts.json 9 def index 10 @posts = Post.all.order(created_at: :desc) 11 end 12 13 # GET /posts/1 14 # GET /posts/1.json 15 def show 16 @post = Post.find_by(id: params[:id]) 17 @user = @post.user 18 end 19 20 # GET /posts/new 21 def new 22 @post = Post.new 23 end 24 25 # GET /posts/1/edit 26 def edit 27 @post = Post.find_by(id: params[:id]) 28 end 29 30 # POST /posts 31 # POST /posts.json 32 def create 33 @post = Post.new(post_params) 34 35 respond_to do |format| 36 if @post.save 37 format.html { redirect_to @post, notice: '投稿しました。' } 38 format.json { render :show, status: :created, location: @post } 39 else 40 format.html { render :new } 41 format.json { render json: @post.errors, status: :unprocessable_entity } 42 end 43 end 44 end 45 46 # PATCH/PUT /posts/1 47 # PATCH/PUT /posts/1.json 48 def update 49 respond_to do |format| 50 if @post.update(post_params) 51 format.html { redirect_to @post, notice: '投稿を更新しました。' } 52 format.json { render :show, status: :ok, location: @post } 53 else 54 format.html { render :edit } 55 format.json { render json: @post.errors, status: :unprocessable_entity } 56 end 57 end 58 end 59 60 # DELETE /posts/1 61 # DELETE /posts/1.json 62 def destroy 63 @post.destroy 64 respond_to do |format| 65 format.html { redirect_to posts_url, notice: '投稿を削除しました。' } 66 format.json { head :no_content } 67 end 68 end 69 70 71 #private 72 # Use callbacks to share common setup or constraints between actions. 73 #def set_post 74 # @user = User.find(params[:id]) 75 #end 76 77 # Only allow a list of trusted parameters through. 78 #def post_params 79 # params.require(:post).permit(:title, :body, :image,) 80 #end 81 82 def ensure_correct_user 83 @post = Post.find_by(id: params[:id]) 84 if @post.user_id != @current_user.id 85 flash[:notice] = "権限がありません" 86 redirect_to("/posts/index") 87 end 88 end 89end 90

Ruby

1/*views*/ 2 3<%= form_with(model: post, local: true) do |form| %> 4 <% if post.errors.any? %> 5 <div id="error_explanation"> 6 <h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> 7 8 <ul> 9 <% post.errors.full_messages.each do |message| %> 10 <li><%= message %></li> 11 <% end %> 12 </ul> 13 </div> 14 <% end %> 15 16 <div class="field"> 17 <%= form.label :タイトル %> 18 <%= form.text_field :title %> 19 </div> 20 21 <div class="field"> 22 <%= form.label :内容 %> 23 <%= form.text_area :body %> 24 </div> 25 26 <div class="field"> 27 <%= form.label :画像 %> 28 <%= form.file_field :image %> 29 </div> 30 31 <div class="actions"> 32 <%= form.submit value="投稿"%> 33 </div> 34<% end %> 35

Ruby

1#routes 2 3Rails.application.routes.draw do 4 resources :posts 5 6 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 7 get "signup" => "users#new" 8 post "login" => "users#login" 9 post "logout" => "users#logout" 10 get "login" => "users#login_form" 11 12 root 'posts#index' 13end 14

補足情報(FW/ツールのバージョンなど)

macOS Catalina 10.15.4
rails 6.0.2.2
Ruby 2.6.3p62

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

エラーメッセージのとおりです。クラス内でpost_paramメソッドがコメントアウトされているため、呼び出せません。

投稿2020/04/20 05:05

maisumakun

総合スコア145121

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問