How to load assets in Camaleon Cms Rails? - ruby-on-rails

I have installed a custom theme in Camaleon Cms and put the images in the following folder:
app/apps/themes/<theme name>/assets/images/
When I try to load the image by using the following path:
<%= image_tag("logo.png", :alt => "logo", :width => "", :height => "") %>
then it looks for the image inside main asstes folder of the project i.e.:
app/assets/images/
but I want to look it inside the assets folder of the theme I installed.
Anybody knows how to do this?

add image load path in the tag to load image from your selected theme folder
<%= image_tag theme_asset_path("images/logo.png"), :title=> "logo" %>

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 image_tag auto prefixes "/images" before #user.avatar.url instead of "/assets" in paperclip

I am using paperclip to upload user avatar.
Here is User model:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "user.png", :path => "app/assets/images/:class/:attachment/:id/:basename_:style.:extension", :url => ":class/:attachment/:id/:basename_:style.:extension"
So image is saving in app/assets/images/user/avatar/:id/:basename_:style.:extension
But when I do
<%= image_tag #user.avatar.url %>
It shown as:
<img src="/images/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original">
i.e. inserts "/images" instead of "/assets"
I double-checked, the avatar image exists in the assets/images/user/avatar/ folder
Although all other images in page are showing correctly using assets pipeline "/assets/logo-thebighashgohere.png"
NOTE: This works correctly if I manually insert image url as string
i.e.:
<%= image_tag "users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original.jpg" %>
It correctly shows as
<img src="/assets/users/avatars/15/99e88dc27c19d8c6163d9cd305f738be_original-thebighashgohere.jpg" alt="99e88dc27c19d8c6163d9cd305f738be original thebighashgohere">
I strongly recommend you to not save user-generated-content into the assets folder!
if your website is going into production mode, the assets are compiled and everything you throw in at runtime won't be catched.
stuff like that belongs into the /public directory (!)
to solve your problem
:url => ":class/:attachment/:id/:basename_:style.:extension
you tell paperclip how to generate your "url". with "path" you define where the files are stored internally, with url you control how to generate the routes. your rote is wrong, no asstes path in it.
but again - do not save those picutres into assets !
btw: i wonder if your solution is even possible in productionmode. the assetspipeline is generting files with a digest appending on it, while paperclip knows nothing about those digest it will always render a route without the digest-stamp. by that you can't call the images from the assets pipeline. so your whole concept won't work in production, but i might be wrong

Ruby on rails 4 assets images cannot be loaded

Currently, I am using ruby on rails 4.
In the html part, I am trying to load an image:
<img width="121" height="31" src="/assets/images/flatty/logo_lg.svg" />
The image file: logo_lg.svg is in the location: app/assets/images/flatty/
I tried:
<%= image_tag "/assets/images/flatty/logo_lg.png" %>
And in production.rb file: config.serve_static_assets = true.
Moreover, tried: rails assets:precompile
But the system still cannot find the image file.
You have a .svg but you are trying to load a .png . Change your code to this:
<%= image_tag "flatty/logo_lg.svg" %>
Don't use your full directory when you load images. The asset pipeline does that for you. Just put them in the right place i.e the images directory and write down:
<%= image_tag "someimg.png" %>
Additionally, if you want to style your image, you can give it a class or an id ( id would fit more for a logo):
<%= image_tag "flatty/logo_lg.svg" , :id => 'someId' , :class => 'someClass' %>
And then just put the CSS in your application css file or the one generated by the controller of you page:
.someClass {
width: 50px;
height: 50px;
}
#someId {
width: 50px;
height: 50px;
}
If you want to put an SVG though, they react differently. You have to remove the attributes given in the SVG's code (you can open up an .svg file with most of the text editors). It's XML markup.
Read this answer for more info.

Images showing on Heroku with image_tag, but not showing in dev

I have an image that I'm rendering the path with image_tag:
<%= image_tag "piechart.png" %>
...which renders as:
<img src="/images/piechart.png" />
The file resides in app/assets/images/
I can't try to load the image in the browser as it tells me:
No route matches [GET] "/images/piechart.png"
Do I need to edit the routes.rb file for assets? As mentioned, it shows in Heroku (but as /assets/piechart-4f7638b49b22f54811bb6cffc3171810.png)
Thanks

How to the use the image_tag with a remote URL?

I've got a rake task which uploads images I've cached from an API to my S3 bucket. In my view, I try to output the image but it just doesn't appear to work. What I want to do is cache the images onto my filesystem, send them to S3 and I want to use the location of the image from my S3 bucket rather than my filesystem. My code looks like below:
In my rails console, I do this just to check the image url:
1.9.3p125 :002 > a.image
=> http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629
1.9.3p125 :003 >
I use Paperclip in my app, is it supposed to add the url as "http:://"? Seems rather weird. The code in my index.html.erb looks like this:
<li>
<%= movie.title %>
<%= image_tag movie.image.url %>
</li>
But this results in the following html:
<li>
Cowboy Bebop
<img alt="1" src="/assets/http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629">
</li>
Why does it include the '/assets'/ before my URL?
I configured Paperclip to set up the image url for my European S3 Bucket following a tutorial. So in my environment.rb, I've got this:
#Signature correction for Paperclip and AWS
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"
And I've got an aws-signature.rb file in my initialisers directory with this code:
#Makes Paperclip use the correct URL for images
Paperclip.interpolates(:s3_eu_url) { |attachment, style|
"#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}
There's a problem with the URL : http::// instead of http:// so image_tag doesn't know it's an absolute URL.
How do you generate these URLs? Gem or your own code?

Resources