# App design question: user\_photo helper

**URL:** <https://www.ruby-forum.com/t/app-design-question-user-photo-helper/130552>\
**Category:** Rails\
**Created:** [February 29, 2008, 10:30am UTC](https://www.ruby-forum.com/t/app-design-question-user-photo-helper/130552 "2008-02-29T10:30:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [February 29, 2008, 10:30am UTC](https://www.ruby-forum.com/t/app-design-question-user-photo-helper/130552/1 "2008-02-29T10:30:39Z")

</div>

I’m having trouble using an application helper and understanding how I  
should make it work throughout my application - so if you can help me  
with this I’ll be overcoming a big hurdle in my learning.

Here’s the helper method;

[code=]# application\_helper.rb

def user\_photo  
if @user.photo?  
return @user.photo.public\_filename(:thumb)  
else  
return ‘rails.png’  
end  
end[/code]

Pretty simple. I understand what it does, and it works just fine in my  
show action in the user controller;

[code=]# user\_controller.rb

def show  
@user = User.find(params[:id])  
end[/code]

[code=]# show.html.erb

## \<%= @user.login %\>

 \<%= image\_tag user\_photo %\>

Email: \<%= @user.email %\>

Joined: \<%= @user.created\_at.to\_s(:long) %\>

Posts: \<%= @user.posts.count %\>

[/code]

However, I want this helper to work elsewhere in the app, such as a  
\_post partial, which obviously is called on the Post index and show  
actions, etc. But because the user\_photo method uses the @user instance  
variable, I need to make it work when @user isn’t set in the current  
controller, e.g. the posts index action (if I use the user\_photo in the  
\_post partial on the post index action, I inevitably get a nil object  
because @user.photo doesn’t exist);

[code=]def index  
@posts = Post.list(params[:page])

respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @posts }  
end  
end[/code]

I’m already pulling @posts on the post index (and there is a  
‘post.user.photo’ in every ‘post’ in @posts); do I need to define the  
@user instance variable in all the necessary controller actions, by  
doing something like this;

[code=]def index  
@posts = Post.list(params[:page])  
@user = “post.user for every post in @posts” # something along those  
lines…

respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @posts }  
format.rss { render :rss =\> @posts }  
end  
end[/code]

Or can I do this in a different way which doesn’t involve defining @user  
in every controller action ?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [February 29, 2008, 10:57am UTC](https://www.ruby-forum.com/t/app-design-question-user-photo-helper/130552/2 "2008-02-29T10:57:50Z")

</div>

why not just pass the object to your helper method as an argument?

def user\_photo(user)  
user.photo? ? user.photo.public\_filename(:thumb) : ‘default.png’  
end

then in your view template just use:

\<%= user\_photo(@user) %\>

Mike

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [February 29, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/app-design-question-user-photo-helper/130552/3 "2008-02-29T12:55:45Z")

</div>

Thanks Mike. I’ve tried your suggestion but it doesn’t work - definitely  
due to my lack of understanding. On the posts index, I get the  
following;

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.photo?

I replaced the previous helper with your much more streamlined version.  
I also put the \<%= user\_photo(@user) %\> in place of \<%= link\_to  
image\_tag user\_photo %\> in the \_post partial (I’ll put the link\_to and  
image\_tag in when it’s all working). But the post index action isn’t  
passing a @user variable (as seen in the original post, above), and the  
\_post partial currently works by calling ‘post.user’, or  
‘post.created\_at.to\_s(:short)’ local variables on the @posts :collection  
passed on the index view;

## Latest Posts

\<%= render :partial =\> ‘post’, :collection =\> @posts %\>

\<%= will\_paginate @posts %\>

How does the @user variable fit in to this? Wouldn’t it need to be  
defined in either the application helper or the Posts index action?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [February 29, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/app-design-question-user-photo-helper/130552/4 "2008-02-29T14:52:56Z")

</div>

Robin, thanks for the explanation. A user has many posts, and a post  
belongs to a user. I’m still getting an error from the \_post partial;

“undefined method `photo?’ for #User:0x3656eec”

application\_helper.rb

def user\_photo(user)  
user.photo? ? user.photo.public\_filename(:thumb) : ‘rails.png’  
end

\_post.html.erb

\<%= user\_photo(post.user) %\>

I don’t understand why I’m getting the error, because I can link to a  
post.user.photo as follows;

\<%= link\_to image\_tag(post.user.photo.public\_filename(:thumb)),  
user\_path(post.user) %\>

And that successfully passes the ‘post.user’ to the user\_path. Surely  
I’m misunderstanding how the helper uses the post.user to fetch the  
photo…

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [February 29, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/app-design-question-user-photo-helper/130552/5 "2008-02-29T14:15:16Z")

</div>

On Feb 29, 11:55 am, Neil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How does the @user variable fit in to this? Wouldn’t it need to be  
> defined in either the application helper or the Posts index action?

If you want to display a photo that is associated with a user, you  
will need to have access to the user object for that photo.

Looking at your original post, it appears that a post belongs to a  
user - is this correct? If so, in the posts index action for example,  
you would call Mike’s suggested method as user\_photo(post.user). What  
this is doing is passing the user object associated with that post  
through to the user\_photo helper.

Regards

Robin
