colbern
colbern

Reputation: 21

Ruby on Rails: linking to a user-uploaded image in the layout

The following is code to display each of a user's messages, along with the photo associated with each message. The code is located in a partial view from the home_controller.

<% msgs.each do |msg| %>
    <%- if msg.photo? -%>
         <%= image_tag msg.photo.url(:listsize) %>
    <%- end -%>
<%- end -%>

I can display the photo related to a particular message like this. The photo is uploaded by the user via paperclip in rails. The photo is stored in a column on the msg table in the database.

How can I display the latest photo uploaded by the user in the layout itself?

i.e something like image_tag current_user.lastest.msg.photo.url(:listsize) But again it needs to display in the layout, not in the view.

Upvotes: 0

Views: 356

Answers (1)

hayesgm
hayesgm

Reputation: 9086

You can, in fact, use:

 image_tag current_user.lastest.msg.photo.url(:listsize)

in your layout. Obviously, you'll want to be careful with whether a user is or is not logged in. Some code akin to:

 <%= image_tag current_user.lastest.msg.photo.url(:listsize) if current_user.present? %>

will work wonders. Is there any other reason you're afraid to put this in the layout?

Upvotes: 1

Related Questions