Rails forcing the file name with send_data - ruby-on-rails

I've a Clients controller:
class ClientsController < ApplicationController
def index
#clients = Client.all
respond_to do |format|
format.text do
file_name = "clients_emails.txt"
send_data #clients.pluck(:email).join('; '), file_name: file_name
end
end
end
end
There is a link in the application that points to this controller index action :
link_to "Extract Email List", clients_path(format: "text")
When I press the link the file is automatically downloaded but the name of the file is clients.text
It looks like :file_name option is being ignored.
Is there a way with send_data to force the browser to create a file with a given name?

Try it with filename:
send_data #clients.pluck(:email).join('; '), filename: file_name

Related

Rails 6 ActiveStorage Attach file url not working

I have an application where a user can upload a PDF document, when the pdf document is uploaded (with a front-end Stimulus controller using ActiveStorage DirectUpload), I then process the PDF to strip content out to place into an Excel Spreadsheet. Everything is uploaded to an s3 bucket.
My model looks like this:
class Export < ApplicationRecord
has_one_attached :pdf
has_one_attached :spreadsheet
validates :pdf, attached: true, content_type: 'application/pdf'
def process_pdf!
self.pdf.open(tmpdir: "#{Rails.root}/tmp") do |file|
tmpfile = File.open(file.path)
file_name = File.basename(file.path).sub(".pdf", "")
op = PdfScraper.call(pdf_file: tmpfile, output_name: file_name)
attach_spreadsheet(file_name: file_name)
end
end
private
def attach_spreadsheet(file_name:)
self.spreadsheet.attach(
io: File.open("#{Rails.root}/tmp/#{file_name}.xlsx"),
filename: "#{file_name}.xlsx",
identify: false,
content_type: "application/vnd.ms-excel"
)
end
end
The controller looks like this:
class ExportsController < ApplicationController
def create
#export = Export.new(export_params)
#export.uuid = cookies[:visitor_uuid]
respond_to do |format|
if #export.save
#export.process_pdf!
flash[:notice] = "Success!"
format.html { redirect_to download_path }
else
flash[:error] = #export.errors
format.html { redirect_to root_path }
end
end
end
private
def export_params
params.require(:export).permit(:pdf)
end
end
When the redirect happens back to the download_path, I am finding the Export based on the cookie ID but when I attempt to inspect (or view) the download URL for the attached spreadsheet, I am getting the following error:
undefined method `persisted?' for nil:NilClass
The View looks like:
<p>
<%= polymorphic_url(#export.spreadsheet) %>
</p>
I even tried the rails_blob_url(#export.spreadsheet) and it doesn't work. If I look up the URL via the rails console, it seems to work.

Why rails download csv file when I want to show html using render :text or html?

It's simple..
when I write it in my controller like this.
def index
#articles = News.order(:id)
respond_to do |format|
format.html
format.csv { render text: #articles.to_csv}
end
end
and in news.rb
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |product|
csv << product.attributes.values_at(*column_names)
end
end
end
What I expected when I open my site(http://localhost:3000/mycontroller.csv) is shows the text in my screen.
However, it just download csvfile..... Even when I changed render text to html.
What is wrong in my code?
You can try this code
class HomeController < ApplicationController
respond_to :html, :json, :csv
def index
#modelnames = Modelname.all
respond_with(#modelnames)
end
end

Rails Exporting CSV ignore blanks

I'm trying to create a simple CSV export in Rails. The export works fine except when deleting/archiving table items. I'm guessing this is because the export is encountering blanks.
This is working:
= link_to transactions_path(format: :csv) do
Except when there is a item missing from the transaction.
Tried this
= link_to transactions_path(format: :csv,skip_blanks: true) do
but I still get a ERR_INVALID_RESPONSE when calling the export
TransactionController:
respond_to :html, :json, :csv
def index
#shops = current_user.shops
respond_with(#shops) do |format|
format.csv do
response.headers['Content-Type'] = 'text/csv'
response.headers['Content-Disposition'] = "attachment; filename=transactions-#{Time.now.strftime('%Y%m%d%H%M')}.csv"
render inline: #shops.to_csv
end
end
end
Any suggestions?
Change to_csv to pass skip_blanks.
#shops.to_csv(skip_blanks: true)
If you want a link to download a CSV file, you should use send_data instead. If you want to display the file in the browser, use render text: #shops.to_csv
http://railscasts.com/episodes/362-exporting-csv-and-excel
respond_to :html, :json, :csv
def index
#shops = current_user.shops
respond_with(#shops) do |format|
format.csv do
send_data #shops.to_csv, type:'text/csv', filename: "transactions-#{Time.now.strftime('%Y%m%d%H%M')}.csv"
end
end
end
Change your link_to back to what you had before.
= link_to transactions_path(format: :csv)
Maybe using send_data:
def index
respond_to do |format|
format.csv { send_data #current_user.shops.to_csv, filename: "transactions-#{Time.now.strftime('%Y%m%d%H%M')}.csv" }
end
end
Also, is the to_csv an instance method?

Generate PDF's with Prawn

My objective is generate every month a PDF.
I am using Prawn Gem.
Likes this:
../Generatepdf/July2015.pdf
../Generatepdf/August2015.pdf
../Generatepdf/Setember2015.pdf
etc...
So my problem is how to define the urls?
I got a controller "Generatepdf" with a view "index.html"
I only can access of my PDF via:
../generatepdf/index.pdf
Controller:
class GeneratepdfController < ApplicationController
def index
respond_to do |format|
format.html
format.pdf do
pdf = ConsumptionsPdf.new
send_data pdf.render, filename: "lol2.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
end
Class:
class ConsumptionsPdf < Prawn::Document
def initialize
super
text "example"
end
end

Ruby on Rails & Prawn PDF - Create Customer List

I'm trying to produce a PDF report with Prawn, I can get it to do a report on a show action easily enough by passing the single ID but I want to produce one with every record in it. Like a standard rails scaffold index page. Using rails it would look like this:
<% #customer.each do |customer| %>
<%= customer.id %>
<%= customer.name %>
<%end%>
Easy!
But I'm not sure how to do this with Prawn..
Something like:
def index
#customer = Customer.all
respond_to do |format|
format.html
Prawn::Document.generate("customer_list.pdf") do |pdf|
pdf.text "#{#customer.id} "
pdf.text "#{#customer.name} "
end
end
end
Which clearly isn't right.
Any ideas? Thank you.
It's easy to do with Prawn, Gemfile => gem 'prawn', bundle
lets say you have Customer model:
customers_controller.rb
def show
#customer = Customer.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = CustomerPdf.new(#customer)
send_data pdf.render, filename: "customer_#{id}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
then just create pdfs folder under the apps directory, and create file customer_pdf.rb
class CustomerPdf< Prawn::Document
def initialize(customer)
super()
#customer = customer
text "Id\##{#customer.id}"
text "Name\##{#customer.name}"
end
end
show.html.erb
<div class="pdf_link">
<%= link_to "E-version", customer_path(#customer, :format => "pdf") %>
</div>
EDIT:
and don't forget to include pdf to config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
I think the good solution of your problem is custom renderer. The best approach was described by Jose Valim (!Rails core developer) in his book. The beginning of the first chapter is available for free here. This chapter is really what you need.
This how I do it:
class CustomerPdf< Prawn::Document
def initialize(customer)
super(page_size: "A4", page_layout: :portrait)
#customers = customer
bullet_list
end
def bullet_list
#customers.each do |customer|
text "•#{customer.id}- #{customer.name} ", style: :bold
move_down 5
end
end
end

Resources