Ruby on Rails, image does not load - ruby-on-rails

I am attempting to do some basic Rails. I am trying to load an image from the app/assets/images folder using the following code:
<%= image_tag 'image.jpg'%>
It generates the following address (I am using localhost:3000):
http://localhost:3000/images/image.jpg
I have attempted all of the different iterations of Rails images I could find including using <img> tag with src="/assets/images/image.jpg" etc. All with no luck.
Also attempted changing production.rb (edit: and development.rb) to have the following code:
config.serve_static_assets = true
config.assets.compile = true
Following the localhost, link gives an error
"No route matches [GET] "/images/image.jpg""

The public path that Sprockets uses by default is /assets.
This can be changed to something else:
config.assets.prefix = "/some_other_path"
from rails guides
So your image must be available at http://localhost:3000/assets/image.jpg
PS: Just as an advice, try to read the guides, that will definitely help you out in future.

Try to remove
config.serve_static_assets = true
config.assets.compile = true
and add
config.assets.debug = true
config.assets.quiet = true
to your development.rb

I figured it out:
To solve a separate issue where I was getting an error (TypeError: Object doesn't support this property or method) on
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
I changed the 'application' to 'default' as per someone's suggestion. It fixed the issue but it seems this change was later causing my image problems. To fix the image problems I changed it back to 'application' and then to fix this original TypeError I removed the require_tree from application.js and it worked. Here's to hoping that doesn't break something else.

Related

Sprockets cannot precompile chartkick

I am using chartkick (added gem 'chartkick' in my Gemfile) and have defined it in my views/layouts/application.html.erb like this:
<%= javascript_include_tag "//www.google.com/jsapi", "chartkick" %>
But on running the server, it results in an error:
Sprockets::Rails::Helper::AssetNotPrecompiled in Main#index
chartkick.js
I am using rails 5.0.7. I tried following the way mentioned in https://chartkick.com/#rails-5-sprockets. But still, it results in the same error.
Can you help me in finding what I'm doing wrong?
Or If anyone could tell me what direction should I head to, that would be awesome as well.
UPDATE: Adding config.assets.check_precompiled_asset = false fixes the issues.
But, adding this line means it will not verify/check precompiled_assets. Which is not a good thing, is it?

Load SVG file with jQuery - Rails

In development it seemed to work well, I used to load the files like this:
$(".div").load("/assets/svg_file.svg");
But now that I have pushed the project to Heroku, it gave me a 404 error inside my console. It also happened with some images I included, but it was fixed by changing the default html image tag to this:
<%= image_tag("logo.png") %>
Since I can't include any ruby code on the client side, how do I do it?
If any user faces the same issue, go to production.rb file (config => environments => production.rb) and change config.assets.compile = false to config.assets.compile = true. This should fix it.

Select rails 3 assets based on environment

In a Rails 3 application, I would like to be able to use unminified javascript and css files in my development environment for debugging and such, and minified versions in production.
I can think of a few hacky solutions, but ideally I'd like javascript_include_tag and friends to automatically select the right asset file.
Ideas?
Add your js normally using
javascript_include_tag
In your development.rb set:
config.assets.compress = false
and in production.rb use:
config.assets.compress = true

Issue with Javascript include in Rails app

I'm using Rails 3.2.8. When the app is deployed access the view that is including a javascript:
<%= javascript_include_tag "epiceditor" %>
Heroku fails with this log:
ActionView::Template::Error (/app/app/assets/javascripts/epiceditor.js.erb has already been required
I've checked some possible solutions, like checking for any reference that may trigger a circular dependency, or simply removing it in case it is being included somewhere else, which isn't. So, if I include it, I get this "has already been included error", if I don't , then the file isn't included at all.
My config/application.rg has this
config.assets.initialize_on_precompile = false
And applications.js has this:
//= require jquery
//= require jquery_ujs
//= require tabs
It might be important to note that the file the tag is referencing is "epiceditor.js.erb", since it has some embedded Rails code that I needed.
Thanks for your help
EDIT:
I believe this is a bug in Sprockets. If I update Rails to 3.2.9rc2, the error is now this:
ActionView::Template::Error (Asset logical path has no extension: epiceditor/.js
but of course the extension in epiceditor is epiceditor.js.erb, and I've tried being explicit about it in the javascript_include_tag as well.
I found the bug.
It turns out that inside the .js.erb file I'm calling
<% asset_path 'epiceditor/' %>
which should expand to the path where all the epiceditor file are placed, but instead is actually loading the file itself in recursive manner. This is expanding properly in the development environment but not in the production environment. Funny, right?
The reason for this is that is adding a digest. So I fixed the whole issue with this:
<%= asset_path 'epiceditor/', :digest => false %>
and now it does expand to the directory, and doesn't fall into the recursion trap.
Hope this saves some time for someone!

Include Assets Only If They Exist

In our current rails app, we are following certain patterns for including assets such as scripts and stylesheets.
For instance, one such pattern is (code inside the layout):
= stylesheet_link_tag controller.controller_name
The problem here is that not all of the controllers are going to have associated stylesheets. What is the best way to check if an asset exists? Specifically, I know there is some trickery here due to the cache busting asset names.
Finally figured this one out. Asset existence can be checked as follows:
YourApp::Application.assets.find_asset("#{asset}.css").nil?
The answer would then be:
= stylesheet_link_tag controller.controller_name if YourApp::Application.assets.find_asset("#{controller.controller_name}.css")
I had found this answer before and we'd been happily using find_asset in our application (Rails 3.2.16), until one day it started bombing out.
After some digging, it turns out that even in a production environment, with asset precompilation enabled, the first call to find_asset will attempt to actually compile the asset it's looking for, even if that asset has been precompiled already. As you can imagine, this is not good news -- in our specific case, we were pulling in a Compass library file into a stylesheet, which worked in dev and during precompile, but not in production, where Compass was not in the assets load path.
Long story short, find_asset is not a bulletproof way to determine if an asset is available to include. You can read a bunch more about it in the issue someone tried to file about this, and which was subsequently closed as not a bug: https://github.com/sstephenson/sprockets/issues/411
The real way to determine if an asset exists, and which works in both compile and precompile modes is demonstrated in the hoops that the filer of the above issues needed to jump through. Here's the diff for his fix: https://github.com/fphilipe/premailer-rails/pull/55/files
I'm putting this here in hopes that other Googlers who find this don't fall into the same trap I did!
To include an asset based on controller name
<% controller_asset = controller.controller_name %>
<%= stylesheet_link_tag controller_asset if YourApp::Application.assets.find_asset(controller_asset) %>
To include an asset based on controller name and action name (was useful for me)
<% action_asset = "#{controller.controller_name}/#{controller.action_name}" %>
<%= stylesheet_link_tag action_asset if YourApp::Application.assets.find_asset(action_asset) %>
And of course it'd be better not to leave this code as it is, but rather place it in a helper.
= stylesheet_link_tag controller.controller_name if File.exists?(File.join(Rails.public_path, 'assets', "#{controller.controller_name}.css"))
Simple ViewHelpers
This is what I use myself. Add this to your ApplicationHelper:
module ApplicationHelper
def controller_stylesheet(opts = { media: :all })
if Rails.application.assets.find_asset("#{params[:controller]}.css")
stylesheet_link_tag(params[:controller], opts)
end
end
def controller_javascript(opts = {})
if Rails.application.assets.find_asset("#{params[:controller]}.js")
javascript_include_tag(params[:controller], opts)
end
end
end
and use them like this in your application.html.haml:
= controller_stylesheet
= controller_javascript
Note: This works with all .js, .coffee, .css, .scss even though it just says .css and .js

Resources