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.
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?
Related
I am trying to generate links in my nav-bar based on records.
I want to post a link in the patrols section of my app that correspond to a patrol route that an admin generated?
I have tried to place
#patrol_routes = PatrolRoute.all in the application controller
and then i want something like
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path %> so that it takes me to the show page of the patrol route i want to access?
<% end %>
Is this possible? i have tried to google and research it, but I'm not finding anything, perhaps I'm not hitting the correct key words?
Assuming you have a route (as in config/routes.rb) named patrol_route you should do:
<% #patrol_routes.each do |patrol_route| %>
<%= link_to patrol_route.name, patrol_route_path(patrol_route) %>
<% end %>
postI'm trying to retrieve related posts by tags by showing the attached image per related post, uploaded by using the carrierwave gem
Tags were written from scratch but similar to acts-as-taggable-on gem.
Below solution picked from Rails count article per tag and related article by tag displays the related posts by title.
<% #posts.each do |post| %>
<% post.tags.each do |tag| %>
RELATED POSTS:
<% tag.posts.each do |related_post| %>
<%= link_to related_post.title + " , ", post %>
<% end %>
<% end %>
<% end %>
Now I want related posts to be displayed by image instead of title per above. Code displaying image for post is
<%= image_tag(post.cover.url(:thumb)) if post.cover? %>
How can I fit this in the RELATED POSTS above?
I tried
<%= image_tag(related_post.cover.url(:thumb)) if post.cover? %>
but this throws:
NameError: undefined local variable or method 'related_post' for #<#:0x007fb67db38fb8>
Obviously related_post is not recorded in the carrierwave uploader model since it is different from the tags model. Any help please?
I'm trying to get my miniatures view to display all associated photos from the collections model.
My sample miniature has two photos but the following code gives an error "undefined method `photo' for #".
<% #miniature.collections(:photo).each do |photo| %>
<%= image_tag #miniature.collections.photo.url(:medium) %>
<% end %>
I think that the relationships are all correct though because rails console works fine with them and the following code shows the first image, twice.
<% #miniature.collections(:photo).each do |photo| %>
<%= image_tag #miniature.collections.first.photo.url(:medium) %>
<% end %>
Equally I can swap out first for last and show the 2nd image twice. What am I doing wrong?
Without knowing a huge amount about your associations, I'm relatively sure you want to use the instance variable that you instantiated in the each loop. That would look like something to the effect of:
<% #miniature.collections(:photo).each do |collection| %>
<% if !collection.photo.url(:medium).nil? %>
<%= image_tag collection.photo.url(:medium) %>
<% end %>
<% end %>
Using #miniature.collections.first.photo.url(:medium) would display the first image of the collection once for each photo in the collection. Likely this is not what you want.
I'm simply trying to ensure a value exists in a certain block and to display it's contents if it does and if not display "Nothing for this yet." I've researched other SO posts but my implementation must be off.
<% #profiles.each do |profile| %>
<%= profile.current_club %>
<% if profile.listings %>
<%= video_thumb_embed(profile.listings.last.video).html_safe %>
<% end %>
<% end %>
Video is an attribute of the listings model.
I'm trying to display all user profiles on an index page and to display the latest video they have added on this index page.
It errors out if a user hasn't added a listing so I added what I thought would be a simple check in the each block but it errors out with the below
undefined method `video' for nil:NilClass
What am I not understanding here? With the above I expect a profile would show on the index page with a video if it had one and if it didn't it wouldn't display anything.
You want to check and make sure that profile.listings is not blank or empty. If you call the last method on an empty array, it returns nil.
<% if profile.listings %> will return true even if you have an empty array.
<% if !profile.listings.blank? %> or <% unless profile.listings.blank? %> should do the trick.
Rails: How can I add admin approve functionality where user uploaded images have to be approved before they are displayed on their profiles?
How about add a column to the user's avatar that says admin_approved or something similar? Since you're using ActiveAdmin, it should pick up the column (unless you've specified otherwise). You then add a check before displaying the image:
<%= user_images.each do |image| %>
<% if image.admin_approved %>
<%= image_tag image.url %>
<% end %>
<% end %>