I am very new to ruby on rails and web development in general. I am trying to display a PDF in the browser using pdf.js within my rails app from a link. I have created a new folder in the assets folder for PDFs and have this link in my page <%= link_to "PDF", root_path << "/assets/PDF.pdf" %>
which is where it is stored. I keep getting an error when I click the link that says 'The action 'PDF' could not be found for PrivateController'. I'm assuming that this is a routing issue, but I have no idea how to change the routing to make the PDF be found. This is my default route match ':controller(/:action(/:id))', :via => :get
. Please explain your answer like I have no idea what I'm doing.
Asked
Active
Viewed 629 times
0

Xenedra
- 69
- 12
-
1Paste your routes.rb file so we can see what routes you have set up. It's important to note that Rails will start looking for a matching route from the top of the list down and immediately follow the first matching route it finds. Post your routes so we can see what's happening, thanks. – bkunzi01 Apr 11 '17 at 16:31
-
Rails.application.routes.draw do root "public#index" match ':controller(/:action(/:id))', :via => :get end – Xenedra Apr 11 '17 at 16:33
-
Thanks Jess. Next time edit your answer and paste it so it retains it's view format (fine here since you only have two routes). Now, you want to setup your link_to differently. Since you have a controller named privates and we'll assume you have an action 'pdf' you'll want to link to it as: <%= link_to "View PDF", controller: 'privates', action: 'pdf' %> – bkunzi01 Apr 11 '17 at 16:48
-
I still get the same error. What should be listed under my action in the private controller? – Xenedra Apr 11 '17 at 17:45
1 Answers
0
First of all include your folder PDF in assets path
And in both config/environments/development.rb and config/environments/production.rb set:
config.assets.paths << Rails.root.join('app', 'assets', 'PDF')
app.config.assets.precompile << /\.(?:svg|eot|woff|ttf|pdf|jpg|png)$/
Now you can use path as
<%= link_to "PDF", assets_path("your-pdf-file.pdf") %>
If you don't want to use PDF as assets create the folder PDF inside your_app_path/public folder and access files as
<%= link_to "PDF", "/PDF/your_file_name.pdf" %>
Hope this helps

Mayank
- 727
- 6
- 9
-
Thank you! However, now the PDF is being automatically downloaded instead of being displayed in the browser. It seems like it is not using the PDF.js functionality. – Xenedra Apr 11 '17 at 17:57
-