How can I render send to a client a file (PDF, CSV, etc) without render this file?
For example, imagine a Students controller (created using scaffold), we have ‘new’ form and ‘create’ action:
def new
@student = Student.new
respond_to do |format|
format.html # new.html.erb
end
end
def create
@student = Student.new(params[:student])
respond_to do |format|
if @student.save
flash[:notice] = 'Student created'
format.html { redirect_to(@student) }
else
format.html { render :action => "new" }
end
end
end
When a student is successfully created it will redirect to ‘show’ template. That is OK!
But I need to send to client -for example- a PDF file and then render ‘show’ action.
This PDF file is like a creation receipt for client.
Extra info: now use Prawn to make PDF and sending to client by code like this:
respond_to do |format|
format.pdf { render :layout => false }
end
In brief, I need get filled form, create student, send PDF to browser (as a creation receipt) and render ‘show’ action to display student created.
Thank you very much.
Here is my solution using wicked_pdf.
This creates the pdf but doesn’t download it. Saves PDF to a file. Saves the link in the database, rerenders the view with the link displayed (view is displaying the @plan).