user1454373
user1454373

Reputation: 370

undefined method `title' for nil:NilClass guides.rubyonrails.org/getting_started.html

I'm new to Rails and am working through the Ruby on Rails getting started tutorial. I'm getting an error on 5.7 Showing Posts that says 'undefined method title' for nil:NilClass'. Any assistance you can provide would be greatly appreciated.

class PostsController < ApplicationController
def new
end

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

    @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



<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<p>
  <strong>Text:</strong>
  <%= @post.text %>
</p>

Upvotes: 1

Views: 2175

Answers (2)

phocks
phocks

Reputation: 3273

Thank you, this worked for me. Moved the "show" definition to above the private block. I think that the order of the Getting Started doc may have been confusing.

Upvotes: 0

Mike Szyndel
Mike Szyndel

Reputation: 10593

Your show method is private, you need to move it above the keyword.

In the future you may prefer to write

def some_method
  ...
end
private :some_method

to avoid this.

Upvotes: 4

Related Questions