Jump to content

I was working on this rails tutorial: "http://guides.rubyonrails.org/getting_started.html" and than I got to the part that you are supposed to show the title and data for an individual post (5.7) and got an error when it was supposed to show the data, so I put @post.inspect into /app/views/posts/show.html.erb and I got nil, and same with the index page where it lists all of the posts, but I checked and the data is in the database correctly.(in case this helps, on part 4.3 where you're supposed to uncomment # root to: "welcome#index", the file said #root "welcome#index" instead, even though I'm using rails 4.0 and ruby 2.0) Here's my controller file:

 

class PostsController < ApplicationController
def new
end

def create
@post = Post.new(post_params.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
def index
@posts = Post.all
end
end

 

here's the error:

 

NoMethodError in Posts#show
Showing /home/hiram/rails/meme/app/views/posts/show.html.erb where line #3 raised:

undefined method `title' for nil:NilClass
Extracted source (around line #3):
1 <p>
2 <strong>Title:</strong>
3 <%= @post.title %>
4 </p>
5
6 <p>

Rails.root: /home/hiram/rails/meme

 

Link to comment
https://linustechtips.com/topic/40124-problem-with-ruby-on-rails/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×