Using URL variables in 'show' view - ruby-on-rails

I'm working on a Ruby on Rails project that uses Paperclip for file uploads, S3 for storage, and does some back-end image conversion using Blitline. The result of the conversion gives the original and a file called upload.png in my S3 bucket alongside the original.
So, after conversion I've got two files something along the lines of:
myaws.amazonaws.com/mybucket/model_id/original.pdf and
myaws.amazonaws.com/mybucket/model_id/upload.png
Ideally, I would like to keep the original at hand in my bucket case my user needs to download it again, or if we need to do another conversion for some reason.
Is there a method similar to <% = image_tag #attachment.url %> that will specify the file 'upload.png'?
Edit (More info:)
I did attempt <% = image_tag #attachment.url, :format => :png %> though it does not work. Seems as if rails is still trying to pull it up as a PDF

Have you specified a style for your attachment? If you have one, let's say xyz then you can get the url <% = image_tag #attachment.url(:xyz) %>

Related

Rails. Preview document from S3 server

I uploaded pdf documents on s3 using carrierwave and fog. Is there a way for users to preview content without downloading it?
By the end of the day i realised that i might have not only pdf but other types of files. So in fog.rb i set public to false so it generates uniqe url's
config.fog_public = false
And in my view.html.erb i do a simple link_to so it would open document in a new window without saving it like this:
<%= link_to "View CV", #applicant.cover.attachment.url, :target => "_blank" %>
Maybe there is way to preview links but for the moment that's fine.
You might consider using PDF-JS. There is a Rails gem but it hasn't been updated in 2 years. I would probably just download the latest version.

Rails4 | Paperclip: image is uploaded but it doesn't displays well (strange url is returned)

I'm using paperclip to upload images in rails, the images are saved well, but then, <%= #user.avatar.url %> returns this:
/system/users/avatars/000/000/001/original/1000203288934_DOCF635653TS102451125.gif%3F1416704056
/system/users/avatars/000/000/001/original/10407722_1175881652452049_8262371134443675175_n.jpg%3F1416705182
instead of just:
/system/users/avatars/000/000/001/original/1000203288934_DOCF635653TS102451125.gif
/system/users/avatars/000/000/001/original/10407722_1175881652452049_8262371134443675175_n.jpg
It happens for every image I upload. Where the hell that %3F-whatever at the end of the url comes from? What I'm doing wrong?
That is what Paperclip uses for cache-busting. If you want to remove it, just call #user.avatar.url(:original, timestamp: false)
You can also disable this globally by putting the following in the config/initializers/paperclip.rb file.
Paperclip::Attachment.default_options[:use_timestamp] = false
This answer explains why you may want cache-busting in place though.

Rails If - Else Statement image_url to check existence

<div class="img-container">
<%= if #user.image_url.present? %>
<%= image_tag #user.image_url(:main).to_s %>
<%= end %>
</div>
I am using rails 4, and carrier wave to upload photos as mentioned in the rails casts. So I have a column in my db that is called "image", and the above code works without the if statement. When I use the #user.image_url(:main).to_s and it has an image it properly shows the image in the container. I want to upload a standard photo when the user does not provide one. It's located in my assets/images folder.
How can I get the if statement to detect if there is photo present or not in the column image? I have to use image_url if showing the user uploaded photo. Not just image to display the image, and the .to_s is a safety net. Any thoughts or answers?
Thanks!
Thanks to the accepted answer, I did use the suggested carrier wave solution for Rails 4 which is recommended for 3.1 and above. This post helped me get it corrected: Default URL not loading with Carrierwave in Rails
Specifying a default url with CarrierWave should do the trick. This allows for a fallback if no image is currently present.
Example:
def default_url
ActionController::Base.helpers.asset_path "fallback/main/default.jpg"
end
For Rails 5 the one that worked for me is
ActionController::Base.helpers.resolve_asset_path("logos/smthg.png")
returns nil if the asset is absent
and
the asset path if present
👍

Image not showing despite being hardcoded - Rails

Rails 4.1
Ruby 2.1.1
I have the following in my view:
<% img_loc = "#{Rails.root}/app/assets/images/cw/agent/avatar/#{agent.id.to_s}/thumb_#{a.img_name}"%>
<td><%= image_tag "#{img_loc}" %></td>
When I run this on my production server, the image does not show. My production log shows the following:
INFO -- : Started GET "/home/dnc/app/assets/images/cw/agent/avatar/11/thumb_myimage.jpg"
FATAL -- :
ActionController::RoutingError (No route matches [GET] "/home/dnc/app/assets/images/cw/agent/avatar/11/thumb_myimage.jpg"):
But when I check, the file /home/dnc/app/assets/images/cw/agent/avatar/11/thumb_myimage.jpg does exist. Any ideas?
Path Helpers
One has to question why you're not using the asset path helpers to give you access to the images directory:
<% img_loc = image_path("cw/agent/avatar/11/thumb_myimage.jpg") %>
--
Image Tag
Alternatively, and probably more appropriately, you should look at using image_tag with a relative path, as this will load the image you need without having to hard-code it:
<%= image_tag "cw/agent/avatar/11/thumb_myimage.jpg" %>
I understand your reasoning (you want to access images directly), but I don't get your method. Rails has a very robust relative path system, which should give you the flexibility to access the images as you need

Rails 3.2 link_to url from database without quotes?

Please help... :) Working on this for a couple of days now
I have a submittal database with stored url's to manufacturer installation instructions generally in PDF formats. I don't want to use the default to_path because I'll have to store the documents locally on my server. Here's the snippet of code that I'm using to pull the url path from my database. As you can see below without the path_to rails wants to turn the url into a path (see error below). I tried adding quotes but then rails doesn't read the code correctly. I've read about helpers but haven't gotten them to work.
<%= #wf_room.wf_lights.pluck(:typemark).count %> <%= link_to #wf_room.wf_lights.map(&:typemark).uniq, #wf_room.wf_lights.map(&:url).uniq %>
error:
undefined method `http://www.sistemalux.com/en/files/ficheproduit/7050_Sliver_wall(13).pdf_path' for #<#:0x0000000404bad8>
Have you tried string interpolation?
<%= link_to #wf_room.wf_lights.map(&:typemark).uniq.first, "#{#wf_room.wf_lights.map(&:url).uniq.first}" %>
(I added first method because won't they the current methods return arrays?)

Resources