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
Related
I am writing a Ruby on Rails 5 app. I just learned how to upload an image without using paperclip,carrierwave, or refile. I have used refile in the past but for this app, I wanted to learn how to do it with out those third party gems. And it was not that difficult to do. I have successfully uploaded a file. My problem is accessing the image afterwards! I thought this would be rather simple. But NO!
I have image files being uploaded to "/public/images/page/image.jpg"
I have added "/public/images/page" to the assets path in app/initializers/assets.rb
I have tried straight img tag like this:
<img class="pic left" src="/public/images/page/mars01.jpg" alt="Mars01" />
I get a "(No route matches [GET] "/public/images/page/mars01.jpg")" error.
I have tried:
<%= image_tag "/public/images/page/#{#page.image}", class: 'pic left' %>
basically the same as tag, get same results. Also tried:
<img class="pic left" src='<%= image_path "#{#page.image}" %>' />
still get routing error, but only looking in "/images" directory. I am still in development mode, so I thought that image_path would be the correct path.
What is the correct way of accessing an image, that was just uploaded to a directory outside of the asset pipeline? That will work in development or production env? I really thought this would be straight forward.
You don't have to give the whole path to image within image_tag, you just give the image name with the sub directory if it exists.
So try <%= image_tag "#{#page.image}", class: 'pic left' %>.
And please read more about asset pipeline.
http://guides.rubyonrails.org/asset_pipeline.html
Here is what I ended up doing for anyone interested.
in my controller I did this:
# default to development go to app/assets/images
path = Rails.root.join('app', 'assets', 'images', uploaded_io.original_filename)
if ENV['RAILS_ENV'] == 'production'
# change only if in production to public/assets/images
Rails.root.join('public', 'assets', 'images', uploaded_io.original_filename)
end
File.open(path, 'wb') do |file|
file.write(uploaded_io.read)
end
Based on RAILS_ENV; if development, images will go to app/assests/images folder; if in production they go to public/assets/images.
And using a straight forward image_tag:
<%= image_tag "#{#page.image}" %>
worked like a charm. Well at least I know it works in development setting.
Thanks to those that responded. (Mat, and Nagasaki)
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) %>
<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
👍
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?)
how to display the image, which stored outside from the project directory?
Is there a simple way to do it?
I see two ways :
On top of your rails server, in production and sometimes in dev, you have to use a web server like apache or nginx. With these, you can serve files from other directories for specific URL. E.G. you can make http://yourapp.com/images/ serving files from a specific dir. In rails, display the image with a traditionnal image_tag
Example with Nginx :
# Find the right `server` section which you currently use to serve your rails app
server {
listen 80;
# Add this
location /images {
root /var/www/my/specific/folder;
}
location / {
#...
#... Here you should already some code to proxy to your rails server
}
}
With that, when you access to `yourserver.com/images`, nginx serve your specific folder and not your rails app. Then in your app view :
<%= image_tag 'http://yourserver.com/images/my_pic.jpg' %>
If you can't access your server settings, you can serve an image file from a controller action with send_file
In a controller :
class ImagesController < ApplicationController
def show
send_file File.join('/var/www/my/specific/folder',params[:name]), :disposition => 'inline'
end
end
In config/routes.rb
match '/images/:name' => 'images#show', :as => :custom_image
Then when you access this action (via the route you defined in config/routes.rb), you have the image. So in your view you do a traditionnal image_tag with this URL :
<%= image_tag custom_image_path( 'my_pic.jpg' ) %>
OR
<%= image_tag custom_image_url( 'my_pic.jpg' ) %>
If it's stored outside of the Rails app directory, then it does not belong to asset pipeline and you can simply link to it:
<%= image_tag("http://example.com/some_file.jpg") %>
Obviously it must be accessible through HTTP (you need nginx or Apache server installed).
This is probably bad idea and will lead to a bunch of issues. Some security, some functionality, but most of the effects I actually don't know.
I do know, from experience, that whenever you go against the rails conventions for what and where stuff is, it's a slippery slope of things breaking, best avoided.
Create a solution using the framework provided.
Note this functionality can also be affected a bit if you are using rails 3.1+ as opposed to <= 3.0 due to asset compilation which copies js, css and images into public.