How do I access uploaded images in refinery cms? - ruby-on-rails

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 %>

Related

Hi, i am quite new to web developing. So forgive me for any stupid question

I am trying to build an ecommerce page using rails. In my products page I want to add products with an image, description, color and price. Everything works properly except for the images. I am not able to assign proper image to the desired product. If i use this syntax : <%= image_tag("/assets/6.jpeg", class: "img-fluid") %>, then this particular image is assigned to all other products and it's definitely not logical!
I have already added the images which I need for my project in the app/assets/images folder. I want to have the possibility to dynamically add/modify the photos on my page.
Please help me how to solve this issue. Thanking you guys in advance!
if images file are saved 1.jpeg-?.jpeg you could loop say
<% #product.each_with_index do |product, index| %>
<% i = index + 1 %>
<%= image_tag("assets/#{i}.jpeg"), class: 'image-fluid' %>
<% end %>
I mean for what you're asking this would work but is probably not the best way to go about this.

How to access additional images (other than default_image) in Shoppe?

I am currently accessing the default_image in Shoppe using:
#product.default_image.path
but I can't seem to find the method to access additional images.
I'd like to write something like:
<% #product.image.each do |prodimg| %>
<%= link_to image_tag(prodimg.image.path, :width => '300px'), product_path(product.permalink) %>
<% end %>
I would appreciate any help or pointers.
There actually isn't "additional images" Shoppe uses a gem called "nifty-attachments" which allows you to link an image file directly to your database. In order to add more images you will need to add more "nifty-attachments" to your model. See here: https://github.com/atech/nifty-attachments

Ruby on Rails Youtube Image Embed

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.

How to properly handle "image not found" errors in Rails using Paperclip

I am using Paperclip to store profile pictures for users. I am trying to be quite strict, by allowing only certain file types, etc. Nevertheless it is quite common for users to manage to mishandle something.
In my index.html.erb file I list all user profiles, but it is common for this list to throw Application Error on the production environment, if one of the images is damaged, cannot be found etc, the whole thing fails, and the whole page fails.
Currently I find the image like this
<% #members.each do |member| %>
<tr>
<td><%= image_tag member.avatar.url(:thumb) %></td>
</tr>
<% end %>
How do I wrap this in a code, that would not allow the page to fail, if for some reason the image fails, and would display an alternative image, as specified in PaperClip documentation (aka default image).
I would suggest to write a method in model that checks the condition and call the method from View.
Model:
def image_url(image_size)
avatar_image = member.avatar
if avatar_image.present? && avatar_image.url(image_size).present?
return avatar_image.url(image_size)
else
return "/images/default_image.jpg"
end
end
View:
<%= image_tag member.image_url(:thumb) %>
Try this.
In your model:
has_attached_file :image, :default_url => "/images/no-image.jpg"

Google-maps-for-Rails without a Model

Sorry to be newbie, but i wanna draw your attention to my problem while using gmaps4rails - great gem.
In my app there is no database backend. Actually i just want to collect markers like this:
<% markers=[]%>
<% #search.each do |dish| %>
<% if (not dish.restaurant.nil?)
restaurant=Restaurant.where( :id => dish.restaurant["Id"])
markers<< {:longitude => restaurant.lng , :latitude=> restaurant.lat}
end%>
<div id="dish_listed"><%= render 'dish_listed', :dish => dish %></div>
<% end %>
<%= gmaps4rails(markers.to_json) %>
Unfortunately it displays a gray rectangular whithout a map *(
I`ve managed to follow the steps from Wiki (i did it several times *))))
When i do
markers.to_json
i get
"[{\"longitude\":"30.252442359924316",\"latitude\":"59.92999013067258"}]"
And i have no idea why kind of code
<%= gmaps4rails('[{\"lng\":"30.252442359924316",\"lat\":"59.92999013067258"}]') %>
doesnt work? Where should it be initialized? Or what magic steps did i forget? I also forked a project by thasuresh. Started it and the removed everything from the model, and pushed my markers there, and it displayed me just what i wanted.
But i still cant reach the same result on the project i write by my self!!!
-----And now i`m sitting here and waiting for flying tomatos *)))-----
PS
rails 3.1rc5
gmaps4rails 0.10.2
BTW Could it be no gmappable model?
This works:
<%= gmaps4rails('[{"lng":"30.252442359924316","lat":"59.92999013067258"}]') %>
There are still little fixes to do for Rails 3.1, some threads are open on github.
To adjust the zoom level with one marker, check this question.

Resources