Alec Wilson
Alec Wilson

Reputation: 566

undefined method `permit' for nil:NilClass in Rails Guide Section 5.7

Working on the Rails Guide Creating a Blog and am stuck at 5.7: Showing Posts.

I have already consulted with this answer:NoMethodError on section 5.7 of Rails Guide, and it solved a different problem for me, but now I am stuck here. It's telling me I never defined the method permit, which I understand, and don't see it defined anywhere, but the guide never had me define it or referenced it needing to be defined. From playing with it and getting some errors back, I gather it is necessary to actually store the post data?

I can get to the point where I submit a post, but when I submit, all I get back is:

NoMethodError in PostsController#create

undefined method `permit' for nil:NilClass

The error message specifies the error occurs in the second line of this code:

  def create    
    @post = Post.new(params[:post].permit(:title, :text))

    @post.save
    redirect_to @post

My posts_controller file looks like this:

class PostsController < ApplicationController
  def new
  end

  def create
    @post = Post.new(params[:post].permit(:title, :text))

    @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

When I remove .permit(:title, :text) from the create method (as suggested here: Can't create new post), I no longer get an error, but am just served a page that reads:

Title:

Text:

Even though I submitted text.

Please let me know if I need to post more info or if I should have found a fix easily, first time poster, brand new to Rails.

Upvotes: 1

Views: 6340

Answers (4)

heemin
heemin

Reputation: 332

There may be an error in your view. (app/views/posts/new.html.erb) It seems like a parameter :post is not passed to the controller properly.

To prevent undefined method error, you can write like params.require(:post).permit(:title, :text)

Upvotes: 1

Farag Elfadaly
Farag Elfadaly

Reputation: 21

When I use version 3.2.13 it don't work To resolve the problem , remove the portion of " .permit(:title, :text) "

Upvotes: 1

Art Smith
Art Smith

Reputation: 77

I figured it out. If you are running Rails version prior to 4.0 (do a "rails --version"), then the guide is wrong... it is evidently providing info that only works in 4.0. The correct statement in the controller file is:

@post = Post.new(params[:post])

Art

Upvotes: 1

Sylvester Willis
Sylvester Willis

Reputation: 62

I've been working with this tutorial, and I figured out what the issue was. When you're doing

@post.update(params[:post].permit(:title, :text))

you are calling the permit function on the POST parameter. This would be fine, except that the if statement is reached when the page is loaded for the first time and the form has not been submitted, therefore the :post key is not inside the params hash, causing a nil:nilClass error. If you add a check for :post inside the hash then do the permit inside the if statement as seen below, then everything should work fine.

if params.has_key?(:post)
    @post.update(params[:post].permit(:title, :text))
    redirect_to @post
else
    render 'edit'
end

Upvotes: 2

Related Questions