Ruby on Rails
Monday, July 29, 2013
On Monday, July 29, 2013 2:15:38 PM UTC-4, Ruby-Forum.com User wrote:
I was working on this rails tutorial:
"http://guides.rubyon...ng_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
--
Posted via http://www.ruby-forum.com/.
The following code:
private
def post_params
params.require(:post).permit(:title, :text)
end
must be at the end of the file. Once you declare private, everything defined after that will be considered private unless you change the declaration. The way you have this coded, the methods show and index will now be private and not available to the view.
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b90f3d98-4074-4872-9fb1-5af6a74c61b6%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment