Rails image_path/url helper methods - ruby-on-rails

I have an image in app/assets/images/portfolio/01-example.jpg
I'm not entirely sure on how to use the image_url or image_path helpers to display this image in the view.
Is it <%= image_path('01-example.jpg') %>? If anyone could correct me with the correct syntax, I'd appreciate it!

Do you want to display it in an image tag, ie is your view html?
Then it's
<%= image_tag('portfolio/01-example.jpg') %>

Related

Ruby on Rails Ckeditor tags shown

I've got an issue with ckeditor. after text posted/page created in preview it shows tags.
How I can fix it and see only edited text itself?
In your view try
<%= raw(#yourvariable) %>
I my application I use Rails .html_safe method for that purpose.
<%= string_from_ckeditor.html_safe %>

Rails image_tag full path

I have images in my assets folder and path to it
"app/assets/images/sets/img_2168.jpg"
so i want to use it like
Dir.glob("app/assets/images/sets/*.jpg") it return me "app/assets/images/sets/img_2168.jpg"
image_tag("app/assets/images/sets/img_2168.jpg") but it gives me empty image
Is it possible if so how to fix it?
Try the followings:
<%= image_tag("sets/" + File.basename(Dir.glob("app/assets/images/sets/*").first))%>
If you just provide "sets/img_2168.jpg" to image_tag, it should work.
You don't need to provide the complete path.
Try this one :
image_tag(File.read("#{Rails.root.to_s + '/assets/images/sets/img_2168.jpg'}").url)
OR
<img src="/assets/images/sets/img_2168.jpg"></img>
Ideally you should pass image object from the controller and in image_tag just get its url like :
image_tag(#image_obj.url)
The easiest way that I have found is to do this: The asset_url tag will put the full url of the asset (in this case an image called: "app-store.png") and then it will be passed to the image_tag normally.
<%= image_tag "#{ asset_url "app-store.png"}" %>
Rails 5 :
<%= image_tag(image_url('sets/img_2168.jpg')) %>
It work for me.

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.

Is there a better way to display the filename in carrierwave?

At the moment
<%= link_to comment.file, comment.file_url %>
displays
/uploads/comment/file/6/IP___Addresses
Is there such thing as something like comment.file.filename ?
Is there a way to get the filename and display a link to that, so it would just say IPAddresses.txt and links to "/uploads/comment/file/6/IPAddresses" ?
Edit:
Figured it out
<%= link_to File.basename(comment.file.url), comment.file_url %>
You could have used the *_identifier method, in your case:
comment.file_identifier

Showing linebreaks in Rails3

I store the linebreaks as "line\n\nline" in the database.
When i am displaying it, I convert it using this method:
def showLineBreaks(from_textarea)
from_textarea.gsub(/\n/,"<br/>")
end
But these renders the text as
line<br><br>line
instead of showing the linebreaks.
What is the right way to do this?
You probably need to flag your content as html_safe for it to display properly, otherwise the view will render it as the string should be displayed.
<%= showLineBreaks.html_safe %>
If you're trying to display newlines saved from text areas, you could do the following in your view:
<%= simple_format from_textarea %>
No need to do manual substitution in this case.

Resources