Images not displaying carriwewave - ruby-on-rails

Windows 8.1
Rails 4.1
Ruby 2.0
I have the CarrierWave
class AvatarUploader < CarrierWave::Uploader::Base
Cw_image_folder = '/agents/uploads/cw/'
def store_dir
"#{Cw_image_folder}#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
This is working for uploads and images as well as their various versions (thumb, small and medium) are stored in the defined store_dir.
The problem is that when I try to use the images, like this:
<img class="media-object" src="<%= #agent.avatar.current_path(:medium) if #agent.avatar? %>" alt="Image Not Found">
or this:
<img class="media-object" src="<%= #agent.avatar.url(:medium) if #agent.avatar? %>" alt="Image Not Found">
The image is not displayed. Searching through the logs, here's what I find:
Started GET "/images/C%3A/agents/uploads/cw/agent/avatar/36/thumb_img_1984.jpg" for 127.0.0.1 at 2014-06-09 03:43:13 -0700
Why is the /images/C%3A added to the beginning?

To store correct path in db, use this
<%= image_tag #agent.avatar.url if #agent.avatar? %>

Related

Rails: get image source after asset precompile

Currently I do this for my website
image_tag("logo.png")
This results
<img src="/assets/logo-c9dc9867ad75fdidmjdoehdo53di.png" alt="Logo" />
This works just fine for me. But sometimes I just need the source part of the image i.e I just need this part /assets/logo-c9dc9867ad75fdidmjdoehdo53di.png. How can I get it?
If your images are in app/assets/images, then use asset_path
<%= asset_path("logo.png") %>
# => "/assets/logo-c9dc9867ad75fdidmjdoehdo53di.png"
If your images are in public/assets, then use image_path
<%= image_path("logo.png") %>
# => "/assets/logo-c9dc9867ad75fdidmjdoehdo53di.png"

Rails 4.2 upgrade: Asset names passed to helpers should not include the "/assets/" prefix

I upgraded from Rails 4.1 to 4.2. I get the following error now:
Sprockets::Rails::Helper::AbsoluteAssetPathError at /
Asset names passed to helpers should not include the "/assets/" prefix. Instead of "/assets/spinner.gif", use "spinner.gif"
The error message is clear. However, I don't know what it's talking about. It highlights this line of code:
<div class="loading">
<%= image_tag asset_path('spinner.gif') %>
</div>
I do not use the literal string '/assets/' in that line of code. So what is this error referring to?
I was able to resolve that specific error by removing the call to asset_path and just using image_tag 'spinner.gif'; however, I still get the error right here (I am using Paperclip gem):
<%= image_tag current_user.avatar.url(:thumb) %>
caused by this:
ActionController::Base.helpers.asset_path('missing-user.png')
Again, it is complaining about asset_path.
UPDATE:
Error only occurs when I pass asset_path to image_tag method:
ActionController::Base.helpers.asset_path('missing-user.png')
=> "/assets/missing-user.png"
helper.image_tag(ActionController::Base.helpers.asset_path('missing-user.png'))
Sprockets::Rails::Helper::AbsoluteAssetPathError: Asset names passed to helpers should not include the "/assets/" prefix. Instead of "/assets/missing-user.png", use "missing-user.png"
image_tag will pass the source option to asset_path on its own:
image_tag("icon")
# => <img alt="Icon" src="/assets/icon" />
image_tag("icon.png")
# => <img alt="Icon" src="/assets/icon.png" />
image_tag("/icons/icon.gif", height: '32', width: '32')
# => <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
So when you call image_tag asset_path('spinner.gif') you're actually doing image_tag( '/assets/spinner.gif' ) which is why you get the sprockets warning.
I resolved the issue but I still don't understand the WHY factor. This only happens when I upgraded from Rails 4.1 to 4.2. Check this out:
ActionController::Base.helpers.asset_path('missing-user.png')
=> "/assets/missing-user.png"
helper.image_tag "/assets/missing-user.png"
Sprockets::Rails::Helper::AbsoluteAssetPathError: Asset names passed to helpers should not include the "/assets/" prefix. Instead of "/assets/missing-user.png", use "missing-user.png"
helper.image_tag "missing-user.png"
=> "<img src=\"/assets/missing-user.png\" alt=\"Missing user\" />"
Based on the above, image_tag does not want you to pass it the literal path string 'assets'. Consequently, in my Paperclip gem helper, I had to od this:
has_attached_file :avatar,
styles: { normal: "128x128>", thumb: "40x40>" },
default_style: :thumb,
default_url: ->(attachment) { 'missing-user.png' }
In other words, I had to remove this:
ActionController::Base.helpers.image_url('missing-user.png')
since image_url returns the string '/assets/missing-user.png'.

Image src doesn't look like it's going through asset pipeline

I'm going through the Rails Tutorial by Michael Hartl. In one of the chapters he is explaining about the asset pipeline, and that if I enter the link_to helper code with the image_tag like so:
<%= link_to image_tag("rails.png", alt: "Rails logo"), 'http://rubyonrails.org/' %>
It should see produce something similar to this:
<img alt="Rails logo" src="/assets/rails-9308b8f92fea4c19a3a0d8385b494526.png" />
but in my browser I see the actual image directory
<img alt="Rails logo" src="/assets/rails.png" />
Note the page renders fine in my browser, but I'm wondering why the code doesn't look like it does in the book.
Because you're probably viewing the page in the development environment. The hashed version of the image is created when the assets are compiled. This typically happens when you deploy your app to production.

Rails Engines: Accessing an images under an engine

We have a view under an engine that is supposed to show an image. this image (Button-Blank-Red-icon.png) is saved under the engine's path of "app/assets/images/engine_name/Button-Blank-Red-icon.png
I am getting the below error when trying to display this view.
ActionController::RoutingError (No route matches [GET] "/images/<>engine_name>/Button-Blank-Red-icon.png"):
What's the right way to display an image stored under engine's app/assets/images/engine_name/?
Here is the view code:
<img src="<%= RED_GREEN_YELLOW_CLS[r.step_qties.last.ontime_indicator] %>" height="15" width="15" />
r.step_qties.last.ontime_indicator returns one of 'green', 'red' and 'yellow'
The definition of constant RED_GREEN_YELLOW_CLS is in my_constant.rb under engine's config/initializers/:
RED_GREEN_YELLOW_CLS = {
'green' => '/Button-Blank-Green-icon.png',
'red' => '/Button-Blank-Red-icon.png',
'yellow' => '/Button-Blank-Yellow-icon.png'
}
One working solution with image tag (refers to episode#277 in railscasts.com):
<%=image_tag(RED_GREEN_YELLOW_CLS[r.step_qties.last.ontime_indicator], size: '23x23') %>
There is no need of block .
The my_constant.rb is:
RED_GREEN_YELLOW_CLS = {
'green' => 'engine_name/Button-Blank-Green-icon.png',
'red' => 'engine_name/Button-Blank-Red-icon.png',
'yellow' => 'engine_name/Button-Blank-Yellow-icon.png'
}
It looks like you're displaying your image via an AssetUrlHelpers. Your view code needs to be changed to the following:
<img src="<%= image_path(RED_GREEN_YELLOW_CLS[r.step_qties.last.ontime_indicator]) %>"
height="15" width="15" />
image_path is going to properly add the assets portion of the path, along with the hash generated by the asset pipeline, so that you get a correct url.
The correct url should be "/assets/<>engine_name>/Button-Blank-Red-icon.png" because when rails compiles the assets it will just mix all the content of /images /stylesheets and /javascript for all the gems and your app/assets dir inside public/assets. It will just copy "<>engine_name>" folder and it's content there.
It's not clear where this: "/images/<>engine_name>/Button-Blank-Red-icon.png" comes from, I'm not sure how to get that from the code you show.
Try to change what you use to "/assets/<>engine_name>/..." and it should work.
You can try to compile your assets manually and look where the files go, just do "bundle exec rake assets:compile RAILS_ENV=production", you can safely delete /public/assets after that test to keep it clean.
Have you tried asset_path helper?
<%= image_tag(asset_path("engine_name/#{RED_GREEN_YELLOW_CLS[r.step_qties.last.ontime_indicator]}"), size: '23x23') %>
replace engine_name with the actual name of the engine app.
Images from engine are accessible via:
http://localhost:3000/assets/engine_name/image.png
So as you said
<img src="<%= asset_path(RED_GREEN_YELLOW_CLS[r.step_qties.last.ontime_indicator]) %>" height="15" width="15" />
RED_GREEN_YELLOW_CLS = {
'green' => 'engine_name/Button-Blank-Green-icon.png',
'red' => 'engine_name/Button-Blank-Red-icon.png',
'yellow' => 'engine_name/Button-Blank-Yellow-icon.png'
}
Will work.
p.s. I find r.step_qties.last.ontime_indicator personally insulting train wreck. :)

Wickedpdf Heroku some images are not showing

Everthing works perfect on my local, but on production(Heroku) some images in the generated pdf are not showing:
<img alt="Bg_pdf" id="background" src="file:////app/app/assets/images/bg_pdf.jpg">
<img alt="Bg_pdf2" id="background2" src="file:////app/app/assets/images/bg_pdf2.jpg">
<img alt="Bg_pdf2" id="background3" src="file:////app/app/assets/images/bg_pdf2.jpg">
<img alt="Bg_pdf2" id="background4" src="file:////app/app/assets/images/bg_pdf2.jpg">
<img alt="Step_4" id="pic3" src="file:////app/app/assets/images/steps/raka/right/step_4.jpg" style="margin-top: 20px;">
The #pic3 does not appear, but all the other images above appear.
Why does pic3 not appear in the generated pdf? Is there because there is some sort of maximum amount of images allowed?
In my gem file:
gem 'wicked_pdf', github: 'mileszs/wicked_pdf'
gem 'wkhtmltopdf-heroku'
And I am using the helper method:
<%= wicked_pdf_image_tag("steps/#{session[:product]}/#{session[:sving]}/step_4.jpg", :id => 'pic3', :style => 'margin-top: 20px;') %>
Some trouble with .jpg, jpeg images, used .PNG images instead and it works well.

Resources