Rails asset pipeline doesn't serve images - ruby-on-rails

My Rails app doesn't serve images at all.
image_url('picture.jpg')
# will result in url(http://localhost:3000/images/picture.jpg)
# but should be url(http://localhost:3000/assets/picture.jpg)
image_tag 'picture.jpg'
asset_url 'picture.jpg'
# will result in the same url / path as image_url()
Neither http://localhost:3000/images/picture.jpg nor http://localhost:3000/assets/picture.jpg exists, while http://localhost:3000/assets/images/picture.jpg does.
Here is a gist of my application.rb and development.rb: https://gist.github.com/maximski/1ccb75f6f89c02932239
I am in development environment and I don't want to precompile files manually. The app is pretty much much newly generated so the configuration is almost completely set on default.

This problem appear if images doesn't exists in app/assets/images directory. Check that app/assets/images/picture.jpg file is exists.

Related

Print the names of all assets in the Asset Pipeline?

I'm trying to debug why some assets are found and others not in the Asset Pipeline. i've tried a lot of obvious things (like typos, clearing tmp, clean/clobber).
Now I placed binding.pry right before where the image path is generated, and I'd like to view (print to the rails console / debugger) the name of every asset rails thinks is available.
How can I do that?
You can take a look at the manifest:
Rails.application.assets_manifest
# or just the files
# this is empty in some of my apps, no idea why, maybe cache or some
# lazy loading that I'm missing:
Rails.application.assets_manifest.assets
Or maybe loop through asset paths:
Rails.configuration.assets.paths.flat_map{ |path| Dir.glob("#{path}/*.{js,css}") }
Please note, this doesn't answer the question (how to view the available assets in the console/debugger), and therefore shall not be accepted as an answer, but it's a small start: showing how to view assets from the terminal:
rails webpacker:clean
rails webpacker:clobber
rails webpacker:compile
The last step will print all the available assets to the terminal.
Another way is to run bin/webpack from the terminal, and in the output will be each asset. Again, this doesn't provide the list from the console or debugger.

Change pseudo-absolute assets path in Rails 3

I tried every way I found, but I don't know how to do it.
Rails always writes /assets/... and would like to have ./assets/ or assets/ because my application is in a subfolder, not in the root folder.
I tried with config.action_controller.asset_host = "." but Rails writes http://./assets/
I tried with config.assets.prefix = "." but Rails writes /./assets/
Suppose that my application lives in: http://domain.com/example/, with the default behaviour of Rails every path points to http://domain.com/ and not to http://domain.com/example/. I don't want to specify an absolute path, because that is not a portable solution.
Check this as an example: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag
You should be able to edit your environment file (config/environments/development.rb) under Rails 3, adding the line:
config.action_controller.relative_url_root = '/example'
This line allows you to run in a subdirectory called "example." Note that you'll want to duplicate this change in production.rb and test.rb as well.

Asset names passed to helpers should not include the "/assets/" prefix

I have a rails app in production with apache2 and passenger. But some of my images didn't appeared so i changed the path inside the 'image_tag' like this:
<%= link_to image_tag("/assets/#{product.image_url}", {:title => "Push it into cart!"}), line_items_path(product_id: product), method: :post %>
After this all work fine in production. But when i try in development, i got this error:
Sprockets::Rails::Helper::AbsoluteAssetPathError in Store#index
Asset names passed to helpers should not include the "/assets/" prefix. Instead of "/assets/cs.jpg", use "cs.jpg"
What i should do to make it work in both enviroments?
image_tag works without using /assets/ path as long as you have the asset pipeline enabled (which is the default) and your image is in app/assets/images/ folder. If your image is outside that folder it won't get precompiled so you'll have to add it to the asset precompilation, in your config/environments/production.rb add
config.assets.precompile += ['image.jpg']
I think that for your problem you shouldn't store the images of your products in the assets folder as this is used for the application images not for you content (models) images, those should be stored in the public folder public/uploads is commonly used for that by gems like paperclip
You should never use /assets/ prefix, it's possible that you just need to precompile assets for production.
rake assets:precompile RAILS_ENV=production
It also depends on where are you using the said asset, for example, when using it in stylesheet you should use image-url('image.jpg') rails css helper. When using it inline in the view, you use the image_tag helper, as you have.

Set url to be used for assets

I have a rails app
salmonfishing.com
which has a database. I have another app
salmonfishing-test.com
How can I alias the root url so that when a user goes to
http://salmonfishing-test.com
the assets and data from the
http://salmonfishing.com database
will be used.
My database.yml points to the 'salmonfishing' database, but the generated url for an image is
http://salmonfishing-test/bigfish.png
That is as would be expected, however internally I would like that to be aliased to
http://salmonfishing.com/bigfish.png
...basically so all assets and data are served from 'salmonfishing' database, and saved to it. Ran across various ways of setting sub-paths, but not root for assets and data.
Cheers,
Jet
If your only problem is the wrong domain name in your asset URLs, you can change it in your environment config file: config/environments/[whatever].rb:
config.action_controller.asset_host = "http://salmonfishing.com"
This line is already present in production.rb, but it's commented out.

Rails: Images on one server, CSS and Javascript on another

I am working on a rails app that has a bunch (hundreds) of images that are hosted on an S3 server. To have helpers like image_tag point here I had to add this to by config/environments/development.rb test.rb and production.rb:
config.action_controller.asset_host = "http://mybucket.s3.amazonaws.com"
However, this also means that it looks there for CSS and Javascript. This is a huge pain because each time I change the CSS I have to re-upload it to Amazon.
So.. Is there an easy way I can make my app look to Amazon for images, but locally for CSS/Javascript?
(I'm using Rails 3.0)
You can pass a Proc object to config.action_controller.asset_host and have it determine the result programmatically at runtime.
config.action_controller.asset_host = Proc.new do |source|
case source
when /^\/(images|videos|audios)/
"http://mybucket.s3.amazonaws.com"
else
"http://mydomain.com"
end
end
but left as it is, this would give you http://mybucket.s3.amazonaws.com/images/whatever.png when you use image_tag :whatever.
If you want to modify the path as well, you can do something very similar with config.action_controller.asset_path
config.action_controller.asset_path = Proc.new do |path|
path.sub /^\/(images|videos|audios)/, ""
end
which would give you http://mybucket.s3.amazonaws.com/whatever.png combined with the former.
There's nothing stopping you from passing full url to image_tag: image_tag("#{IMAGE_ROOT}/icon.png").
But to me moving static images (icons, backgrounds, etc) to S3 and leaving stylesheets/js files on rails sounds kinda inconsistent. You could either move them all to S3 or setup Apache for caching (if you're afraid users pulling big images will create too much overhead for Rails).
BTW, you don't have to put config.action_controller... into config files for all three environments: placing that line just in config/environment.rb will have the same effect.

Resources