How to write a link to a public file - ruby-on-rails

In my Rails app, I have some files in the /public directory which I want to write hyperlinks to in the views. How do I do this other than writing old HTML like <a href...
Is there a neat link_to way?

There's nothing wrong with using the anchor element, after all that's what link_to outputs anyway. However, you should just be able to do:
<%= link_to 'Some File', '/some_file.zip' %>

Related

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.

How to give the xls file link in Ruby on Rails?

How to give the xls file link in ruby. This is my file path
link_to "Excel", "/#{RAILS_ROOT}/public/reports/10014_ByNetwork.xls", :target=>"_blank"
when i given above link that is converting like this
Excel
So its not working. actually i need like this
Excel
Please give me exact path...
#Gagan Your syntax is incorrect. You should test your answers before posting them. This is the correct way:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", target: "_blank" %>
OR
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", :target=>"_blank" %>
You have missed out the comma after the second closing double quotes.
if you have more than one file to download and you have to use different links along your views I could recommend you the next approach too:
Add a download xls get resource and route helper to your routes.rb file like:
get "downloads/xls/:id" => "downloads#xls", :as => :download_xls
In your controller, for my my example I'll use app/controllers/downloads_controller.rb we will need to add the xls action to stream data with send_file:
def xls
if params[:id]
send_file("#{Rails.root}/public/reports/#{params[:id]}.xls",
filename: "#{params[:id]}.xls",
type: 'application/excel',
disposition: 'attachment')
end
end
You can read more about it here:
http://apidock.com/rails/ActionController/DataStreaming/send_file
And finally in your view you'll use the link_to helper with our declared above download_xls_path route and the filename as param:
<p>
Click to download: </br>
<%= link_to "NameOfYourXlsFile.xls", download_xls_path(NameOfYourXlsFile) %>
</p>
Are you sure that you need file:// link? It is not operated by rails server and will work only on your machine. Maybe it will be better to generate link like this: "http://you_server_url.org/public/file.xml". It works fine on local and remote app, and you can manage file-sending by controller action.
Try this in your view file:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls" :target=>"_blank" %>

Variable + HTML to Button + Ruby on Rails views

I am having a an issue.
<%= link_to "<button>Add</button>".html_safe, new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
What if I want to add a ruby variable and some normal text the button? e.g. $25,- (where the $ and ,- are fixed and the 25 variable...I am quite new to this...sorry if this is too easy, but struggling. I tried a lot of options and googled for long time.
The correct helper you want for this is button_to.
To use a variable in the button text is defined as string iterpolation. In your example it could be something like:
<%= button_to "$#{cost}", new_admin_course_path %>
Check out the button_to api reference for more options.
you could do like
amount = 25 #amount is the ruby variable
<%= link_to "<button>Add $#{amount}</button>".html_safe, new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
and I personally dont add <button></button> to the link and I rather use CSS to get the look and feel
Put your link inside a <button></button> tag. It'll be more readable.
And use variables interpolation in double-quoted strings to insert variable's value into string (the #{#variable_name} part)
<button>
<%= link_to "$#{variable_name}", new_admin_course_path, :id=>"open-contacts-dialog-btn", :class=>"inbox-sf-add-btn tip" %>
</button>

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.

Add image to layout in ruby on rails

I would like to add an image in my template for my ruby on rails project where i currenly have the code <img src="../../../public/images/rss.jpg" alt="rss feed" /> in a the layout stores.html.erb file however this doesn't seem to load as it looks like its missing a route which i'm not sure what its supposed to be.
Any ideas please?
Anything in the public folder is accessible at the root path (/) so change your img tag to read:
<img src="/images/rss.jpg" alt="rss feed" />
If you wanted to use a rails tag, use this:
<%= image_tag("rss.jpg", :alt => "rss feed") %>
In a Ruby on Rails project by default the root of the HTML source for the server is the public directory. So your link would be:
<img src="images/rss.jpg" alt="rss feed" />
But it is best practice in a Rails project to use the built in helper:
<%= image_tag("rss.jpg", :alt => "rss feed") %>
That will create the correct image link plus if you ever add assert servers, etc it will work with those.
When using the new ruby, the image folder will go to asset folder on folder app
after placing your images in image folder, use
<%=image_tag("example_image.png", alt: "Example Image")%>
simple just use the img tag helper. Rails knows to look in the images folder in the asset pipeline, you can use it like this
<%= image_tag "image.jpg" %>
It's working for me:
<%= image_tag( root_url + "images/rss.jpg", size: "50x50", :alt => "rss feed") -%>
image_tag is the best way to do the job friend

Resources