简体   繁体   中英

NameError in PostsController#create undefined local variable or method `post_params' for #<PostsController:0x007fd289176ae8>

Posts Controller

class PostsController < ApplicationController
  def index
  end

  def new 
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)

    if @post.save
     redirect_to @post
    else
     render `new`
   end
 end

  def show
   @post = Post.find(params[:id])
  end

  private

  def post post_params
    params.require(:post).permit(:title, :body)
  end   
end

Here your method name is post and it takes an argument post_params . But in your code you intend to use post_params as the method name.

So change this:

def post post_params
 params.require(:post).permit(:title, :body)
end 

to:

def post_params
 params.require(:post).permit(:title, :body)
end 

You have a type here:

def post post_params
  params.require(:post).permit(:title, :body)
end

Remove post :

def post_params
  params.require(:post).permit(:title, :body)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM