<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
👍
Related
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) %>
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.
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.
Hello and sorry for such a basic question, but I have exhausted all search options.
My question is how do I find an image path? I am starting to learn Ruby on Rails and a gem I am trying to integrate requires me to specify an image path:
colors = Miro::DominantColors.new('/path/to/local/image.jpg')
How do I find the path to a local image saved to my desktop?
I am trying to use the Miro RoR gem (https://github.com/jonbuda/miro).
Thank yo,
Brian
You're going to have to upload the image
Images
The problem you've got is Rails is server-side
You're trying to load an image from your client-side system (your desktop)
This means that in order to get Rails to process the image in any way, you will have to first upload the image into the system, then process it. The recommended way to do this is to use the Paperclip gem (for image uploads), then you can use your Miro gem to process the image
Here's what I'd do:
Upload
You'll first have to upload the image
There are numerous ways to do this, but the process is the same:
Create image model & db (to store the image)
Create upload form
Process upload through controller
Model
#app/models/image.rb
Class Image < ActiveRecord::Base
has_attached_file :image
end
Controller
#app/controllers/images_controller.rb
def new
#image = Image.new
end
def create
#image = Image.new(image_params)
#image.save
end
private
def image_params
params.require(:image).permit(:image)
end
View
#app/views/images/new.html.erb
<%= form_for #image do |f| %>
<%= f.file_field :image %>
<% end %>
Process
After saving the image, you can then call one of Paperclip's processing methods to manage the uploaded image's processing. Here are some ideas on how you may do this:
Paperclip Processing Tutorial
Paperclip Image Processing using DelayedJob
Thanks for the advice and links to paperclip tutorials, Rich. I'll definitely be using those resources in the near future.
The solution was too simple, and I feel guilty for asking the question haha. All I had to do was use terminal to navigate to the image then just $pwd to get the path.
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?)