Forum: Ruby on Rails Making a link to a User profile

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee4ead180a47f98999854b3e1eef7638?d=identicon&s=25 Sean Six (seansix)
on 2009-05-01 04:36
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
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-05-01 07:28
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 Six 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
Ee4ead180a47f98999854b3e1eef7638?d=identicon&s=25 Sean Six (seansix)
on 2009-05-01 11:12
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.
This topic is locked and can not be replied to.