So I have a blog like setup for my website. Users can comment on
another person’s blog. I want the comment to include a link to the
author’s profile. This is proving to be problematic.
The blog is a resource and the comments are also a resource, nested in
the blog.
The profile route is this: map.profile ‘profile/:screen_name’,
:controller => ‘profile’, :action => ‘show’
So if I have a profile by user Steve, it should link to /profile/steve
If Steve leaves a comment on a blog, I want to link to his profile. I
have tried something like <%= link_to comment.user.screen_name,
profile_for(comment.user) %> and variations of it with no success.
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.screen_name
map.profile is a named route, so it’ll give you the route “profile_path”
- I’m not 100% sure which would work, but I’d try:
profile_path(comment.user)
or
profile_path(comment.user.screen_name)
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.screen_name
When is this error appearing? When you load the page that contains that
link, or when you follow that link? If it’s the former, check to make
sure that the user and comment models are associated (has_many, has_one,
belongs_to…). If it’s the latter, then we’d need more information,
like what’s in the “show” action of the profile controller.
Sean S. wrote:
So I have a blog like setup for my website. Users can comment on
another person’s blog. I want the comment to include a link to the
author’s profile. This is proving to be problematic.
The blog is a resource and the comments are also a resource, nested in
the blog.
The profile route is this: map.profile ‘profile/:screen_name’,
:controller => ‘profile’, :action => ‘show’
So if I have a profile by user Steve, it should link to /profile/steve
If Steve leaves a comment on a blog, I want to link to his profile. I
have tried something like <%= link_to comment.user.screen_name,
profile_for(comment.user) %> and variations of it with no success.
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.screen_name
The profile/(screen_name) path works find on its own. When I try to use
in it the blog controller I get problems.
I defined the profile_for in a helper file and included the helper in
the appropriate controller.
def profile_for(user)
profile_url(:screen_name => user.screen_name)
end
When I use <%= link_to profile_path(comment.user.screen_name) %> I get
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.screen_name
If I use <%= link_to profile_path(comment.user) %> it just makes a link
to blog page.