0

I am trying to access the database and enter some info. it gives me an error saying "undefined local variable or method `post_params' for #< PostsController:0x00000005aad728>"

i know this has been answered here. but i tried following what they did, and it just does not work, can someone help me with this?

class PostsController < ApplicationController
  def index
     @post = Post.all     
  end

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

  def new
      @post = Post.new
  end

  def create
      @post = Post.new(post_params)

      if @post.save
          redirect_to posts_path, :notice => "Your post was saved"
      else
          render ="new"
      end

      private
      def post_params
         params.require(:post).permit(:title, :content)
      end
  end
end
Community
  • 1
  • 1
Doctor06
  • 677
  • 1
  • 13
  • 28

2 Answers2

4

Your end for create method is enclosing the private keyword and post_params method. Update it as:

def create
      @post = Post.new(post_params)

      if @post.save
          redirect_to posts_path, :notice => "Your post was saved"
      else
          render ="new"
      end
end # Add end here

private
def post_params
     params.require(:post).permit(:title, :content)
end
end # Remove this end
vee
  • 38,255
  • 7
  • 74
  • 78
1

You define your post_params method inside of create method. Move it outside of it and all will be working.

Sergey Moiseev
  • 2,953
  • 2
  • 24
  • 28