Ruby on Rails Paperclip open in new window - ruby-on-rails

I'm using PaperClip with this code:
link_to "OPEN", #worequest.attach.url
Is there a way to open the file in a new browser tab?
Thanks

To send a file:
You can use the send_data or send_file method in your controller action:
The 'disposition' option specifies whether the file will be shown inline or downloaded. Valid values are ‘inline’ and ‘attachment’ (default).
so this line in a controller's action will open the image in the browser (and not download it):
def get_image
send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
end
To open a link in a new tab:
You can also provide a :target => '_blank' to the Link_to (will open the link in a new tab)
link_to "New tab", url, :target => '_blank'

Related

open a url link in a new tab from a Rails controller

I've been trying to open multiple pdf's in new tabs automatically from my rails controller, but nothing has worked so far so I'm back to square one. If anyone could help, it would be much appreciated! Below is an example of what I'm trying to do.
id_array = [1,2,3]
id_array.each do |id|
// I want to open each of these three links in a new browser tab
http://localhost:3000/pdf/id.pdf
end
Cheers!
Can open files in new browser window using send_file method. Use :disposition => 'inline'
Eg: send_file "#{Rails.root}/pdf/id.pdf",:filename => 'id.pdf', :type => 'application/pdf', :disposition => 'inline'
you should respond with a javascript file.
<% id_array.each do |id| %>
window.open('<%= "http://localhost:3000/pdf/#{id}.pdf" %>', '_blank');
<% end %>

Rails send_data doesn't save file

I'm using Nokogiri to generate an XML file in my app. I want to save this file, and I want to display a dialog box in which the user can select the folder in which download this file.
This is the action in my controller:
def download
require 'nokogiri'
if owner_signed_in?
#slips = current_owner.slips
builder = Nokogiri::XML::Builder.new do |xml|
xml.cedolini{
#slips.each do |slip|
xml.cedolino{
xml.codicecliente_ slip.client_code
xml.data_ slip.day.to_s
xml.ordini{
slip.product_slips.each do |order|
xml.ordine {
xml.codicearticolo_ order.product_code
xml.descrizionearticolo_ order.product_description
xml.ammontare_ order.amount.to_s
}
end
}
}
end
}
end
file = builder.to_xml
send_data file, :type => 'text/xml; charset=UTF-8;', :disposition => "attachment; filename=db.xml"
end
end
I have the route defined in this way:
get '/dbsinc/download'
When I call the action from the view, it doesn't save the XML, I see a new page with the url of my action, and in the page I see the XML file rendered on the page, but it doesn't open any dialog box to save the file.
Where am I getting wrong? Thanks
I think I figured out, route: `post 'dbsinc/download'
And in my view I defined the link in this way:
<%= link_to 'Download ', {controller:'dbsinc', action:'download'}, method: :post %>
And it works, the download dialog opens when I click on the link.

Send_file open file in browse instead of download it

Please tell me how this works, because I don't understand... Here is my simple code:
Route:
get 'download' =>'pages#download'
My action:
def download
send_file "#{Rails.root}/public/downloads/robots.zip", :type=>"application/zip"
And download link:
<%= link_to "download", download_path %>
So, when i'm clicking on the link I get this:
Image http://joxi.ru/GrqvQ3xHlbaYmz.jpg
It seems like the browser is trying to show zip file content... But when I open the action from my browser (or refresh page) it's working fine.
Why doesn't it work when I click on the link generated by link_to?
Adding disposition: "inline" instead of disposition: "attachment" worked for me.
Send_file open file in browse instead of download it
Try with this
def download
send_file("#{Rails.root}/public/downloads/robots.zip", :type=>"application/zip",:disposition=> "attachment; filename=robots.zip")
end
and
<%= link_to "download", download_path ,target: '_self' , data-turbolinks="false" %>

Rails send_data disposition inline doesn't work, csv got auto downloaded instead of open in a new page

I want to allow user to click on a button in my view page and open a new page of a csv file instead of downloading the csv file automatically. This is the button in the view page:
<%= link_to "Export as CSV", {:action => "index", :format => :csv}, :target => '_blank' %>
When the user clicks the above button, it will trigger the index action in my controller. This is part of the controller's index action:
respond_to do |format|
format.html
format.csv do
send_data #resultCSV, :disposition => 'inline', :filename => 'csv_search_results.csv', type: Mime::CSV
end
end
When I clicked on the button in my Chrome browser, it opens a new tab and immediately close that new tab and begins downloading the csv file.
But I used ":disposition => 'inline'". The csv file is meant to be displayed in the new tab. It should not automatically begins the download. This is not what I want and I don't know what is causing it.
How can I fix this issue?
Thanks
Chrome was not intended to display .csv files by default (At time of writing).
Here's a reference from a google search ("cannot view csv chrome"):
https://productforums.google.com/forum/#!msg/chrome/bEncfRIUclQ/Dc4aZL2R7loJ
As you are using Rails, if you really need to display it in the browser you might want to create a view with the parsed but ultimately if you want the users to get a csv you might want the users to download it and open it on their own software.

Download Image attached using Paperclip

Is there a way to make the users download the images attached using paperclip?
I just made a link like this:
link_to 'imagename', image.attachment.url
But this makes the browser open the image. I want the browser to ask the user to save the image. Is there a way to do that?
When it comes to sending a file you can find all information in here http://api.rubyonrails.org/classes/ActionController/Streaming.html There are most important cuts:
Simple download:
send_file '/path/to.zip'
Show a JPEG in the browser:
send_file '/path/to.jpeg', :type => 'image/jpeg', :disposition => 'inline'
Show a 404 page in the browser:
send_file '/path/to/404.html', :type => 'text/html; charset=utf-8', :status => 404
to use one of this option you have to create a new action in controller like this:
class SomeController < ApplicationController
def download_file
send_file image.attachment.path
end
end
Here's a simple solution using the HTML5 download attribute
<%= link_to image.name, image.attachment.url, download: image.attachment.original_filename %>

Resources