On Feb 28, 2012, at 11:58 AM, Christopher Jones wrote:
> Hi all,
>
> In my application all users have the ability to upload a profile picture
> through a file field but it is not compulsory.
>
> On my index page it displays all users along with their profile pictures
> but not all users have a profile picture and therefore some users have
> no image at all.
>
> How would I go about getting a generic image to display for the rest of
> the users.
>
> I have the following section in the index:
>
> <% for user in @users %>
> <tbody>
> <td align = "center"><%= image_tag user.photo.url, :height => 50,
> :width => 50 %></td>
> <td align = "center"><%= user.username %></td>
> <td align = "center"><%= user.favourite_console %></td>
> <td align = "center"><%= user.games.count %></td>
> <td align = "center"><%= link_to "Show", user %></body>
> </tbody>
> <% end %>
>
> For the following line I want to do something like this:
>
> If user has photo
<%- if user.photo.url? -%>
> <td align = "center"><%= image_tag user.photo.url, :height =>
> 50, :width => 50 %></td>
> else
> display this photo
<%= image_tag 'generic_avatar.png' %>
> end
>
> How would I go about doing this?
Ideally, you would extract this into a helper method, since you don't want to be repeating all that conditional logic all over the place. Then you could simply call the 'avatar' method:
#users_helper.rb
def avatar(user)
if user.photo.url?
raw image_tag(user.photo.url)
else
raw image_tag('generic_avatar.png')
end
end
Untested, but it ought to work.
Walter
>
> Thanks
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment