open a url link in a new tab from a Rails controller - ruby-on-rails

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

Related

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.

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.

Ruby on Rails Paperclip open in new window

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'

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

Generate PDF file using AJAX call

I'm trying to generate a PDF file using AJAX call in Rails3. The following code generates a PDF file which I have created using PRAWN gem.
<%= link_to "Generate pdf", books_path(#book, :format => 'pdf') %>
I do not want user to view the PDF until they order it. So, the goal is to create a PDF file in the server.
Any ideas or thoughts much appreciated.
Use this, make sure your remote action does not return the PDF, but simple generates and stores it on the server.
link_to "Generate PDF", prepare_books_path(#book), :remote => true, :method => :put
This will work in Rails 3. If you're using jQuery, make sure to read this article on how to set things up correctly.
Your controller action may look like this:
def prepare
# Do your thing to generate the PDF
render :text => "PDF Generated", :status => 200
end
I used the PUT-method because you are altering the state of your data (e.g. you are generating something new, you don't want a bot or crawler to automatically call that).
Firstly, it beats me why you would do something on a request like generating a PDF, when the user is not expecting that action. Isn't better to only generate the pdf when the user requests for it?
Thanks Ariejan.
I modified your code as following and it did just what I wanted.
<%= link_to "Generate Story Book", pdfbook_stories_path(:format => 'pdf'), :remote => true %>
And for the controller,
def pdfbook
#stories = current_account.stories
respond_to do |format|
format.pdf {}
end
end

Resources