Why does adding ":remote => true" cause my file download not to launch? - ruby-on-rails

I’m using Rails 5. I have set up this link for downloading a file
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), :remote => true %>
which links to this controller method
def download_cc
scenario = Scenario.find(params[:scenario_id])
send_data scenario.cc_data, filename: "#{scenario.title}.imscc", type: 'application/zip', :disposition => 'attachment'
end
But when I click on the link above, even though I see a remote call being made, no file download appears in my browser (I’ve tried this on both Chrome and Firefox). How do I make this work? Note, removing “remote => true” is not an option because then the URL in my address bar changes, which I don’t want.

I've not seen the remote option used with file downloads. It's mostly designed for AJAX form posting and URL hits like deleting a record or action calls that make sense to do over AJAX.
If you want the link to force a "Save As..." dialog, use the download attribute in the link. A 'bare' download attribute will force the "Save As..." dialog. Its value will be the download filename. So <a href="/my/download/action" download="file_100.zip"> opens a dialog prompting you to save file_100.zip.

Related

send_file in rails does not download to browser - instead loads in browser window directly [duplicate]

This question already has an answer here:
Force browser to download file instead of opening it
(1 answer)
Closed 5 years ago.
I have the following in my .erb:
<%= link_to 'Download PDF', planners_download_pdf_url %>
I have the following method to respond:
def download_pdf
send_file(
"#{Rails.root}/Thing/ex/example.xls",
filename: "mything.xls",
type: "application/xls"
)
end
My routes has:
get '/planners/download_pdf'
When I click my link, the method does get invoked. My problem is that is doesn't download to the browser. Instead, it takes the tab I have open in the browser and dumps the file as HTML. Like... have you ever seen an Excel file opened in notepad? It does that. See below:
How do I get this to instead download the file?
You need to add disposition: 'attachment' to your send_file option hash (or at least ensure that you do not have disposition: 'inline' set, since 'attachment' should be the default).
If this is not the issue, if you are using Turbolinks, as mentioned in other answers you need to disable Turbolinks on the link by setting data-turbolinks="false" in your link element (e.g.: data: {turbolinks: false} in your link_to tag helper).
Finally, here are some other things to try if this doesn't work:
Set type to 'application/vnd.ms-excel', the valid MIME type for XLS files.
Set the download="mything.xls" html5 attribute on the link tag directly (e.g.: download: 'mything.xls' in your link_to tag helper.

How do I create a download link that doesn't blink?

I would like the user to be able to download a file from a method I set up in a controller. Further, I don’t want the URL to change in my browser when the user downloads the file. I have this link set up
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), target: '_blank' %>
The problem is, there is a screen blink as a new tab is spawned to generate the download. This looks visually unappealing. I have seen other sites where you click the link and something starts downloading without a blink. How do I do that?
Edit: Here is the function invoked by the link
def download_cc
scenario = Scenario.find(params[:scenario_id])
send_data scenario.cc_data, filename: "#{scenario.title}.imscc", type: 'application/zip', :disposition => 'attachment'
end
I did some local testing and my hypothesis is that Turbolinks is messing things up. I recommend that you remove target: '_blank' from the link and add data: { turbolinks: false } to opt-out of Turbolinks for this particular link. The code after changes should look like this:
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), data: { turbolinks: false } %>
Your controller action looks good and needs no changes.
Just add a "download" attribute to your link:
<%= link_to image_tag("cc_icon.png"), scenario_download_cc_path(subscription.scenario), download: true %>
Remove target
At the moment, you are setting target="_blank", this is telling the browser that when you click the URL, you want to open a new tab. Removing this should just begin the download without opening a tab or window.
< Optional extra info below as I misread your question: >
Formatting the appearance of links
Are you using any HTML or CSS for the design of the site? If so, you can apply css state properties such as "active" "hover" "visited" "link" in your css classes.
Lets say you had a link:
My Link
If you created a css file and customised the properties for the link:
a:visited {
text-decoration: none;
}
that will allow you to alter the appearance of a visited link. You can then use any css property to customise the appearance in any way you want.
Adding CSS files to Ruby app
With Ruby, you can use: <%= stylesheet_link_tag "filename" %> to load in a css file.
Sources:
http://www.w3schools.com/css/css_link.asp
How do I use CSS with a ruby on rails application?
As long as you use send_file you should not see a screen blink.
The scenario_download_cc_path(subscription.scenario) action in your controller should look like this:
file_path = File.join(Rails.root, "/public/myfile.whatever")
send_file file_path
The behavior on Chrome and IE appears the same whether your link does or does not include target: '_blank'

Ruby/Rails - Open a URL From The Controller In New Window

I'm in my applications controller and I have a url string. How do I tell the app to open the url in the browser in a new window. Is there a way to set the target to blank?
For example
def link
#url = 'www.google.com'
****??? Open #url ?? *** with target blank?
end
This is not possible to do directly from the controller. Using redirect_to #url has the effect of opening an URL in the same "window", as it simply sends a HTTP redirect instruction back to the browser. redirect_to is not capable of opening new windows. The controller resides on the server side, and opening a new window belongs to the client side.
Some options:
a) render a link with <%= link_to 'Google', 'google.com', :target => '_blank' %> or Google which the user can click on in a new view
b) use JavaScript to open the link automatically, but beware that browsers may treat this as a popup and block it
By combining these options you can open links in new window for browsers/users who allow it, and fall back to a regular URL in case that didn't work.
As the others point out, you can't (and shouldn't) do this in the controller. In the view you can use
<%= link_to #url, :target => "_blank" %>
Well, it's not that you CAN'T do it, it's just kind of a convoluted process. I did this in a project I'm working on. Basically, it's a mixture of Rails goodness and Javascript. I simply passed a flash notice on creation of an instance, and then used a script to set that flash notice equal to a js variable, and a redirect_url. If that particular flash notice pops up on that page, it redirects in the js script. Like I said, it's hack hack hack, but it works for my purposes.
You can't do that in rails, because your script is being executed on a server.
Use Javascript to work with browser on the client side.

Question about Paperclip for Rails

I'm using Papeclip(2.3.1.1) with Rails(2.3.5).
In my view I write so:
<%= link_to image_tag(p.attachment.url(:small)), p.attachment.url(:original) %>,
and it becomes into
href="/system/attachments/1/original/1.JPG?1270134617 (for a tag)
src="/system/attachments/1/small/1.JPG?1270134617" (for img tag).
And when I click on the picture, my browser (Firefox) offers me to save or open picture and I want to just open picture in browser without any dialogs.
I think it's because link contains ?1270134617 after file's name. How can I fix it?
This could be related with the mime types.
Go to /config/initializers/mime_types.rb, and add (or uncomment, if it's there) this line:
Mime::Type.register "image/jpg", :jpg, ["image/jpeg", "image/pjpeg"]
Then restart your web server.
Hopefully this will make the picture "show on the browser" instead of "trying to download it".

Agile web development with rails - Ajax

i m using rails 2.3.3 and web browser Firefox i have added ajax and java script and it is working too but i have to reload the page every time when i press Add to Cart button to show item additionn in the side bar it don’t show it without reloading.
anyone please help me how can it show item addition in side bar when i press Add to Cart button with out reloading the page?
If you haven't already done so, install Firebug for Firefox, for these reasons:
it'll tell you if you have a Javascript error.
it'll show you if your Ajax request is being received and its contents.
you can inspect your page elements such as the cart to see if it's set to be shown, if the ID is correct, etc. in a much faster way than browsing through the source.
and more (CSS, etc).
If you can't figure it out by looking at the Firebug console, and since you're following a tutorial, why don't you download the Depot source code and compare it with your own to see what you're doing wrong.
If you have the book, the complete source is listed at the end of the book. You can also download the source from here.
The standard ajax helper methods are link_to_remote, form_remote_tag, form_remote_for, button_for_remote. (I might have missed a few.) If you're not using one of them, you could be doing something wrong.
If you're using one of the helper methods with remote as part of the name, you might be missing the update option or the update option is pointed to the wrong html element.
For a form_remote_tag helper:
form_remote_tag(:url => {:controller => controller_name, :action => action_name, :id => id},
:update => element_to_update)
The element_to_update should be the html element's id that you're updating.

Resources