# Making a link to a User profile

**URL:** <https://www.ruby-forum.com/t/making-a-link-to-a-user-profile/166931>\
**Category:** Rails\
**Created:** [May 1, 2009, 4:36am UTC](https://www.ruby-forum.com/t/making-a-link-to-a-user-profile/166931 "2009-05-01T04:36:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 1, 2009, 4:36am UTC](https://www.ruby-forum.com/t/making-a-link-to-a-user-profile/166931/1 "2009-05-01T04:36:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 1, 2009, 7:28am UTC](https://www.ruby-forum.com/t/making-a-link-to-a-user-profile/166931/2 "2009-05-01T07:28:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![df-sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@df-sean](https://www.ruby-forum.com/u/df-sean)\
**Post date:** [May 1, 2009, 11:12am UTC](https://www.ruby-forum.com/t/making-a-link-to-a-user-profile/166931/3 "2009-05-01T11:12:31Z")

</div>

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.
