WifiDuck WifiDuck - 7 months ago 57
Ruby Question

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

dkp dkp
Answer

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 
Comments