Is there a better way to display the filename in carrierwave? - ruby-on-rails

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

Related

How to find the correct path for a link in rails

I got a problem with a link I've tried to make. I want to have a page with an article, and a link to the author page of it. Iv's try 100 differents version but i don't know how to do...
Here is my code :
<a <% link_to " Par : #{#article.author.pseudo}", user_path(User.where(user_id: #author_id) %></a>
or with this path : user_path(Article.find(params[:id]).author_id)
If you know how I can resolve that... Thanks !
Don't wrap link_to helper into <a>tag, it is already generated by the helper. where returns the collection, not the instance - that's why your code does not work. You already have needed user instance in #article.author, just use it
<%= link_to "Par : #{#article.author.pseudo}", user_path(#article.author) %>
You should be able to link directly to the author:
<%= link_to "Par : #{#article.author.pseudo}", #article.author %>
Try something like this:
<%= link_to("Par : #{#article.author.pseudo}", #article.author) %>

How to create a hyperlink without displaying the URL path

How do I create a hyperlink without displaying the URL path?
In my view code, I have something similar to this:
<% #users.each do |user| %>
<%= link_to user.lname, [:edit, user] %>
...
<% end %>
This code works, but produces something like:
Smith (url path)
I just want "Smith"
Not sure exactly what you have going on, or are going for with that array object as the URL target, but I'm betting this will solve your problems:
<%= link_to user.lname, edit_user_path(user) %>
Rails link_to is simply a Helper to create an HTML anchor tag.
You can see in the ActionView Helper Docs that link_to takes 3 parameters.
link_to(body, url, html_options = {})
This generates something similar to...
Smith
If you wish to change your routes, can you give more information like the full URL that's displaying, your routes.rb files, and the rake routes results?
Found the problem. It was in my lousy css. My css was placing the url in the title of the anchor text. I learned a lot about link_to also.

Rails Display Link in Database Using link_to

In Rails, I have a "notifications" class, one field of which is "link". The links contained within this class are formatted like: exchange_path(6), where that is the path to the show action in the exchange controller.
I'm now trying to output this link as such:
<%= link_to "View Exchange", notification.link %>
This line is in a loop which begins as such:
<% #notifications.each do |notification| %>
When I click this link, it takes me to localhost:3000/users/exchange_path(6) instead of localhost:3000/exchanges/6 like I would expect. (The loop generating the faulty link is on localhost:3000/users/2)
this could be scary...
<%= link_to "View Exchange", eval(notification.link) %>
should evaluate and use the path helpers. but you need to be 100% sure that nothing bad gets put in the link field..
You could do this:
<%= link_to("View Exchange", "/#{notification.link.gsub('(', '/').gsub(')', '').gsub('_path', 's')}") %>
or set up a method in your model that formats it for you:
def format_link
link.gsub('(', '/').gsub(')', '').gsub('_path', 's')
end
and just call that in your link_to:
link_to("View Exchanges", notification.format_link)
This will only work if all the links are formatted exactly as the example in the question

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.

Rails, link_to helper with an URL as a param

I want to generate the next html link:
http://url.com
To reproduce it using the link_to helper I have to write:
<%= link_to "http://url.com", "http://url.com" %>
What doesn't look DRY at all, I was expecting this to work:
<%= link_to "http://url.com" %>
But the above code generate a link targeting the actual request.url, not the one I'm sending in the param.
Am I missing something?
You're not missing anything --- the normal case is for the URL and the text that shows to the user to be different.
If you'd like, you could create a helper like
def link_to_href(link, args={})
link_to link, link, args
end
then, when you use it,
<%= link_to_href "http://url.com" %>
Will output
http://url.com
If you take a look at the source code of link_to you will see that at line 248 the a tag label is build with name || url.
That's why you have this behaviour and there is noway to do it like you're expecting.

Resources