Ruby on rails : Display a pdf file - ruby-on-rails

I am uploading PDF files using paperclip, how do I display them after?
This is the error I get:
ActionController::RoutingError (No route matches [GET] "/public/uploads/6/test.pdf"):
I mean ok I know it's a routing error but do I have to define a route for every file? it doesn't make sense to me

You do not have to define a route to it, just link to it properly just like you would an image
Your url linking to the asset is incorrect. Remove the /pulbic so it looks like below.
'/uploads/6/test.pdf'

Related

Using Rails globbing for URL parameters: some URL types get cut off

I'm having an odd problem with Ruby on Rails globbing. My route looks like this:
get 'people/info_from_url/*url', to: 'people#info_from_url'
So from the frontend, I have get requests to URLs like:
my-api.com/people/info_from_url/youtube.com/XXX
my-api.com/people/info_from_url/twitter.com/XXX
my-api.com/people/info_from_url/tinyurl.com/XXX
...
These all work as expected - I get a parameter in the people#info_from_url controller action called url that contains the full URL that was sent.
However, for one particular kind of URL (XXX.carrd.co), the last part gets cut off. In the frontend, I send a get request to my-api.com/people/info_from_url/XXX.carrd.co. From the backend logs:
INFO -- : Started GET "/people/info_from_url/XXX.carrd.co/"
INFO -- : Processing by PeopleController#info_from_url as
INFO -- : Parameters: {"url"=>"XXX.carrd"}
Somewhere, the .co gets dropped. I'm not sure why this could be happening or how to debug it, since the change happens before I'm able to access the params hash. I could deal with this manually by just checking if it's a carrd link, but I'd like to know why it's happening and if any other kind of link might experience this issue. Thanks for any help!
Routes have an implicit optional (.:format) segment at the end. You can use this option to prevent that:
get 'people/info_from_url/*url', to: 'people#info_from_url', format: false
EDIT: you can check the last part here in the docs explaining that too https://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments

Display PDF in iframe in RoR

I would like to display a pdf in an iframe in one of my Rails projects. My current code is <iframe src="tmp/data.pdf"></iframe>, but when I go to this page, I get the error: "No route matches [GET] /Users//development//tmp/data.pdf".
data.pdf is in this location, so I think I'm doing something wrong with my routes file, and maybe I have to route the file to a path appropriately. I've tried playing around with a few things, but haven't had much luck so far. Can anyone provide any help?
Rails only exposes the contents of the "public" subfolder to the webbrowser so no code/configuration can be downloaded by any user...
Try putting the file there and it should work.
UPDATE 1
Also you need to note, that the "public" part does not have to be included in the URL. So in your case the url would be just "/data.pdf".

Access public files in Rails

I want to access the file /public/images/dog.jpg in the Rails project. Is there some URL route that would access it?
I tried public/images/dog.jpg and images/dog.jpg, but it didn't work.
images/dog.jpg is correct.
However I would use the image_path URL route, e.g. image_path('dog.jpg').
See http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

My app cannot load some images from the server

I'm using Ruby on Rails on my local WEBrick server.
I'm generating some images of websites from urls, writing them to my local filesystem, and displaying them. I name the images by their url but replace all \ with -. Some images seem to not be loading because it cannot find the images on my filesystem, and I get the broken image icon. However, I see that all the images are there when I check my filesystem.
This is the error I get in my logs:
Started GET "/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379" for 127.0.0.1 at Thu Jun 30 13:23:06 -0700 2011
ActionController::RoutingError (No route matches "/images/image_site/http:--www.urbandictionary.com-define.php"):
This is my html code:
<img alt="Http:--www.urbandictionary.com-define.php?term=slim%20shady" class="site_image" src="/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379">
What going on and how can I fix this? Please let me know if I need to provide more information.
Looks like you're not properly encoding your image names in the src attribute. I'd guess that you have a file with a name like this:
http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg
But when you have this:
src="/images/image_site/http:--www.urbandictionary.com-define.php?term=slim%20shady.jpg?1309465379"
The filename looks like this:
/images/image_site/http:--www.urbandictionary.com-define.php
because everything after the first ? is considered to be part of the query string.
Replacing the slashes with hyphens is not good enough, you're still leaving all sorts of holes for confusion and nefarious intent. Instead, you should generate the image file names completely, something like id.jpg where id is the image's ID in your database. Then, store the original filename in your database and only show that filename (properly encoded!) to people, don't use it in your local file system.
A quick fix would be to properly URL encode your src attributes. But you really should fix up how you handle the images or you will leave yourself open to all sorts of trouble.
You need to place images in your public/ folder and then you can access them from there. By default Rails will server static assets primarily from public/.

Get server file path with Paperclip

I'm using Rails with Paperclip to make a small file upload app. I would like to be able to return the file path on the server of the uploaded file once its done but I can't seem to work out how to get the path? Paperclip only seems to record the name of the file itself.
Does anybody now how to do this?
Assuming you had an attachment called avatar on an instance of a user, you can use user.avatar.path to get the full path of the file on the filesystem, and you can use user.avatar.url to give the path which you could use in image tags and whatnot.
Is that what you're meaning?
I came cross the same problem, so I made a link to it's url in show.html.erb. It works.
<p>
<b>Pdf:</b><%= link_to "PDF" , #product.pdf.url %>
</p>

Resources