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 %>
Related
I’m using Rails 5. I have this link that a user will click on to download a file …
<%= link_to "#{scenario_file.title}", scenario_file_path(scenario_file) %>
It links off to this controller method …
def show
#scenario_file = ScenarioFile.find(params[:id])
send_data #scenario_file.file_data, filename: "#{#scenario_file.title}", type: #scenario_file.mime_type, :disposition => 'attachment'
end
My problem is, when someone clicks on the link, the browser URL changes to the link (obviously). However, is there any way I can rewrite the above link so that the file will download but my browser’s URL won’t change?
Try to add target="_blank" to link:
<%= link_to "#{scenario_file.title}", scenario_file_path(scenario_file), target: '_blank' %>
Too you can use html5 download attribute. The download attribute is triggering a force download.
<%= link_to scenario_file.title, scenario_file_path(scenario_file), download: scenario_file.title %>
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" %>
I've already asked the question on the Redmine official website but I didn't get any answer, maybe I'll have more chance here.
I'm working on a project and I try to improve an existing plugin for Redmine by adding some features to it to allow the user to upload his Dropbox files in the Redmine documents with a simple form. Redmine already has this possibilty so I would like to use the controller and methods already defined in the Redmine source code.
I have the following code in one of my plugin views:
<% html_title "Reddrop - Sync" %>
<h2>Synchronisation page</h2>
<p>Please choose your file</p>
<%= form_tag({:controller => "documents", :action => "add_attachment", :id => #document}, :multipart => true) do %>
<%= file_field_tag 'attachments[dummy][file]', :id => nil, :multiple => true, :onchange => "addInputFiles(this)" %>
<%= submit_tag(value = "Sync this file with Redmine") %>
<% end %>
I'm calling the "documents" controller and the add_attachment method which are defined in the Redmine source code. When I submit my form I get the following error:
ActionController::RoutingError (No route matches {:controller=>"documents", :action=>"add_attachment", :id=>nil}):
Is it possible to call these controllers/methods through a plugin if they are defined in the Redmine source code?
If yes, maybe could you give some advice to how configure my routes?
See here :id => #document
#document is the variable that contains nil however this should contain some id (like 1,2 or whatever) of required record check it in your controller and once you will fix this issue sure this error will be resolved.
validate route format
rake | rails route
send the expected parameters and the correct namespace. If you already have a #document you could use the form_for.
`<% form_for(#document, url: {action:'add_attachment'} ) do %> `
I'm just trying to setup excel downloading option for my site. All I want to do is:
<%= link_to 'Export', export_to_excel_path(:param => #item.id), url_for(:format => 'xls') %>
I want to give my user a link to export to excel, with the additional param. I get the following error:
undefined method `stringify_keys' for "/controller/show.xls":String
I need to use url_for because in export_to_excel, I have a
respond_to
in order to respond with a .xls MIME type file. I am using the to_xls gem, and everything works fine except that I'm not able to figure out this link.
All credit to jdoe:
<%= link_to 'Export', export_to_excel_path(:param => #item.id, :format => 'xls') %>
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 %>