JoaoP
JoaoP

Reputation: 21

Ruby Tutorial Chapter 5.7

On this guide: http://guides.rubyonrails.org/getting_started.html

On the topic 5.7 Showing Posts, after creating the show.html.erb file I am supposed to get the error: ActiveModel::ForbiddenAttributesError when submiting the form, but instead i get NoMethodError in Posts#show.

Can anyone tell me what i am doing wrong, or a solution to this?

def PostsController < ApplicationController

    def new
    end

    def create
       @post = Post.new(post_params)
       @post.save
       redirect_to @post
    end

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

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

Upvotes: 0

Views: 261

Answers (3)

Tony Baik
Tony Baik

Reputation: 146

When you use private, all of the methods below private is going to be a private method. However, an alternative would be to just say private :post_params in order to make just post_params private.

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Declaring_Visibility

Upvotes: 1

Doon
Doon

Reputation: 20232

your show method is private, move it above the private keyword in your controller and you should be all set. Like below..

def PostsController < ApplicationController

   def new
   end

   def create
     @post = Post.new(post_params)
     @post.save
     redirect_to @post
  end

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

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

end

Upvotes: 3

user2062950
user2062950

Reputation:

Make sure you have a show method defined in your PostsController.

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

Upvotes: 0

Related Questions