The App:
My application has many buses. Each bus has many photos of the interior and exterior. Some of the interior photos have an associated image that indicates the location on the bus where the photos were taken (like a "You are Here" marker on a map), which is assigned through :parent_id attribute that corresponds to the "parent" photos id.
The Goal:
Output the URL for the "child" image if a selected photo is a "parent," and keep blank if not.
The Problem:
The only way I know how to find any particular associated image brings back an array. Unfortunately for me, the photo.url method that comes with Paperclip can't work with an array.
This is closest to what I want, but, again, it brings back an array that I can't use to find the image URL.
def assigned_floorplan(where I pass in all the parent images as a params)
bus_images.all(conditions: { is_floorplan: true, parent_id: params.id })
end
Is this a dead end, or is there a way to pull out the id of the associated image from the array so that I can use the photo.url method? Or am I going about this the wrong way? I'm willing to approach this problem totally differently if you have any suggestions.
You have to call the photo.url method for each image separately. A nested loop would work:
<% #parent_images.each do |parent_image| %>
<%= parent_image.photo.url %>
<% parent_image.child_images.each do |child_image| %>
<%= child_image.photo.url %>
<% end %>
<% end %>
Related
Is there a way to pass an id via checkbox?
Let's say we have Model Record which stores information. Any user who want to edit a record will need to propose it first for the Admin to approve. The problem is Record is linked to multiple Image model (with paperclip attachment) so each Record has_many images. The problem is that I am stuck with how to allow users to propose a deletion of existing photos. I imagine it would be great to store a list of proposed deletion, with image_id and proposal_id to store the deletion waiting for approval but I can't find out a way to program the checkbox.
The checkbox would need to pass in the image_id marked to delete. Is it possible and if so how can I do that? Thanks!
<% #record.images.each do |image| %>
<%= check_box_tag "image_ids[]", image.id %>
<%= image_tag image.url %>
<% end %>
I'm trying to accomplish this without any plugins.
Basically we just use the standard copy and paste method for embedding Youtube onto our website... the problem, then, is that if we try to share a post of ours with video on facebook... the image thumbnail isn't there.
The thumbnail is always saved in this format:
http://i1.ytimg.com/vi/7Uz1hfza55k/default.jpg
...with the video id coming just before "default.jpg"
I already have three "photo" slots in the database for each post.
So I'd like to do something like this:
<%= image_tag("<%= daily.photo .html_safe %>") %>
I'm just not sure of the proper syntax so that it gets the photo URL for that particular post.
What I want it to return in the end is something like this:
<%= image_tag("http://i1.ytimg.com/vi/7Uz1hfza55k/default.jpg") %>
Getting the URL, of course, from the "photo" section of each post's database entry.
For extra credit maybe you could explain a way that I could arrange it so that all the person writing the articles would have to do is enter the video code and it would be automatically inserted in:
<%= image_tag("http://i1.ytimg.com/vi/CODEHERE/default.jpg") %>
Thank you for your time.
Edit:
Just so we're clear this works:
<img src="<%= #daily.photo %>">
But this doesn't work:
<%= image_tag("<%= daily.photo .html_safe %>") %>
They should be the same as far as I know... but they don't work the same. If worse comes to worse I'll just go with img src...
<%= image_tag(#daily.photo) %>
In ERB, <%= stuff %> means: everything inside this is good old plain ruby. There is no need to use the tag twice, as #daily.photo is just an argument for the image_tag method.
All the files uploaded by paper clip are beeing stored inside public/system/images/pictures/ and lots of 000/000/002 folders and etc.
I Want to use image_tag inside one active admin's view to print all the images related to one gallery.
I can get the image object, and of course the image name, but how i figure out the right stored path?
Just call url on the image object, like this:
<% #gallery.images.each do |image| %>
<%= image_tag image.url %>
<% end %>
I'm attempting to create a loop which shows stars as reviews are placed for a product, i have it so that it shows the rating as a number however i wish for it to display an image relating to the rating number,
i've currently got
<%=product.no_of_stars.to_i do image_tag "star-on.png" end %>
however it just displays the rating figure and not the number, no doubt I've missed something simple.
I've researched other questions and they state that should be enough for what i want, but of course its not working as expected.
Thanks, Ben.
The above answer is not quite correct. The problem is that Integer#times returns the integer it was called on, so you will still get 5 as the result. Try
<% product.no_of_starts.to_i.times do %>
<%= image_tag "star-on.png" %>
<% end %>
Try this.
<%=product.no_of_stars.to_i.times do image_tag "star-on.png" end %>
You are missing the times method. This is what allows you to run the number as a loop over and over again (super simplification).
I'm pretty sure that there should be an easy way of doing this. I already tried out to override the models and discovered that the imagines seem to get saved in the database.
All i want it to be able to show all the oploaded images in the application view, so that they can be displayed on every page.
Currently I am new to Rails, I would be thankful for an easy guide or at least some hints.
They're saved as Image model instances and you can use the image_fu view helper to render them in whatever size you need. So in your view just do something along the lines of
<% Image.all.each do |image| %>
<%= image_fu image, "100x100", :id => dom_id(image) %>
<% end %>