On 28 February 2012 16:58, Christopher Jones <lists@ruby-forum.com> 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.
Add a method to model User called image_url (for example) that returns
the actual photo url if there is one or the url of the default image
if there is not.
>
> 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>
Then here use image_tag user.image_url
Always move things down to the model if possible. This has the
additional advantage that you can test it in the model unit test which
is much easier than testing it in the view.
Colin
--
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