How to give the xls file link in Ruby on Rails? - 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" %>

Related

Add arbitrary string to URLs in Rails/Middleman app

I'm using middleman to generate a static webpage. I need to add a consistent but understandable string to all urls so I can understand how users navigate on the page.
Now i do it like this
<% link_to '/'+?button=navigation , class: 'logotype', itemprop: 'url' do %>
...
<% end %>
I would prefer not having to manually add all the parameters but rather just use something that's already there, like a scope or something. I was thinking about using the name of the template file for example. The url is not unique enough.
Any suggestions?
The standard way of doing this would be to write a helper method that encapsulates your functionality:
<%= link_to_as_nav('/', class: 'logotype', ...) do %>
...
<% end %>
Then write a helper method:
def link_to_as_nav(url, options)
link_to(url + '?button=navigation', options)
end
This is the naïve approach and won't account for a url argument that already has parameters added, but that's something you can work to fix.

issue setting up a download link

Very new to rails, so hopefully this is a simple fix. I am trying to set up a link to download a file I store in my website. Here is my download function:
controller
def download
send_file #exam.photo.path, :type => #exam.photo_content_type, :filename => #exam.permalink
end
My routes.rb
resources :exams do
member do
get :download
end
end
and my view
<%= link_to "Download", download_exam_path(#exam.photo.path) %>
I use paperclip to upload the image. I can display it, but I cannot download. Here is my error:
No route matches [GET] "/exams/Users/R45/Programming/rails_projects/oldV_rails_project/public/system/exams/photos/000/000/016/original/Chapter.jpg/download"
Which seems to "tac-on" /download at the of my image link.
The route is wrong in your link. Try:
<%= link_to "Download", download_exam_path(#exam) %>
However since the error message suggests the photo of the exam is in the public path, it might be easiest to simply href the photo directly:
<%= link_to "Download", #exam.photo.path %>

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.

Rails 'link_to' to Download An Image Immediately Instead of Opening it in the Browser

I have a link_to Rails helper that downloads a wallpaper when clicked. But the image is loading in the browser instead of being downloaded immediately.
<%= link_to "1920x1080", #download.wallpapers[1].wallpaper.url %>
But at the same time I have a link_to Rails helper that downloads a screensaver of .exe format but here it works as inteded: file being downloaded immediately.
<%= link_to "720p", #download.screensavers.first.screensaver.url %>
What should I add or do so that the images will not be opened in the browser but instead be downloaded immediately?
Thanks!
There is an easier way to do this with the HTML5 download attribute.
<%= link_to 'Download existing avatar', #user.avatar(:original), download: "User_#{#user.id}_avatar" %>
Instead of putting the link of the image in your tag, you can handle it in your controller. And then in your controller you can do something like
send_file #download.wallpapers[1].wallpaper.url, :type => 'image/jpeg', :disposition => 'attachment'
Read this
Generally, the cleanest way to do this is to set the appropriate header when sending the image:
Content-Disposition: attachment; filename=<file name.ext>
The send_file method will allow you to set this header appropriately if you're serving the file from the filesystem:
http://api.rubyonrails.org/classes/ActionController/Streaming.html#method-i-send_file
If the file is stored in your database, you can use send_data instead:
http://api.rubyonrails.org/classes/ActionController/Streaming.html#method-i-send_data
Rails 3 / 4:
in routes:
get "home/download_pdf"
in controller:
def download_pdf
send_file(
"#{Rails.root}/public/your_file.pdf",
filename: "your_custom_file_name.pdf",
type: "application/pdf"
)
end
in view:
<%= link_to 'Download PDF', home_download_pdf_url %>
Here's a simple solution using the HTML5 download attribute with paperclip
<%= link_to item.name, item.asset.url, download: item.asset.original_filename %>

How to write a link to a public file

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

Resources