Do a download and update the div in a request
I have a requirement to download a PDF file and saving it on clicking a link开发者_StackOverflow. The div has 2 links "View" and "Create PDF". On clicking "Create PDF" link, it should create the PDF, save it in a folder in server and give it as a download. I am doing this now. The problem is I have to update the div so that "View" button will be visible if there is any file in that location, on clicking the "Create PDF" link. i.e if there is no file, "View" link will not be present and on clicking the "Create PDF" link, it should update the div showing "View" link also, apart from saving and downloading the file.
I am using PrinceXML for pdf generation and here is the code I am using now
def create_pdf
user = User.find(params[:id])
data = make_pdf("/scans/create_pdf", user.scan_name.to_s + '_DA_FORM_7425.pdf', true)
File.open(user.pdf_file_path, 'w') { |f| f.write data }
send_file user.pdf_file_path,
:type => 'application/pdf',
:disposition => 'attachment'
end
and in view
<% if File.exists?(user.merge_file_path) %><%= link_to "View", "/scans/view/#{user.id}" %> | <% end %>
<%= link_to "Create PDF", "/scans/create_pdf/#{user.id}"%>
An easier solution would be to put the "View" link in a div and set it's display to none if the File.exists? returns false.
On the onclick event of the other link call javascript that unhides the "View" div. If you are using jquery just use $('#div_id').show()
The other way would be to use an ajax call that returns the html for the link and you update the corresponding div with that html.
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html
精彩评论