hoppdev
hoppdev

Reputation: 47

Remove controller action name from url

I'm having what I feel might be a simple issue but am kind struggling with it. I have an action in my Controller that downloads a pdf when a button is clicked in the View. The PDF download but the issue is after I click it the action remains in the browser and downloads the PDF every time I reload the page.

Example of URL

Before I click button

https://matrix-spam-camp.c9users.io

After I click of the button this stays in URL until I delete it manually from the browser.

https://matrix-spam-camp.c9users.io/download_pdf

/app/app/views/portfolio/main.html.erb (link)

<%= link_to download_pdf_path, class: 'download-btn', remote: true do %>
    <%= image_tag('img_btn_icon.png') %> <span class='resume-btn-text'>Resume</span>
<% end %>

/app/app/controllers/portfolio_controller.rb

def download_pdf
  send_file "#{Rails.root}/app/assets/docs/resume.pdf", type: "application/pdf", x_sendfile: true
end

/app/config/routes.rb

get 'download_pdf', to: "portfolio#download_pdf"

Any help with this issue will be greatly appreciated.

Upvotes: 0

Views: 182

Answers (1)

hoppdev
hoppdev

Reputation: 47

So I was able to solve this by adding a target: _blank to the link_to.

<%= link_to :download, class: 'download-btn', target: "_blank" do %>
    <%= image_tag('img_btn_icon.png') %> <span class='resume-btn-text'>Resume</span>
<% end %>

This prevents the download button from applying the name for the controller action being called by the link_to from being placed in the browsers URL.

Upvotes: 1

Related Questions