I have added assets for a controller and wants to include them per request.
The problem is that it does not include that assets without writing .js in controller path
The code on layout page is:
<%= controller_specific_js = "#{params[:controller]}.js" %>
<%= javascript_include_tag controller_specific_js if asset_exist?
(controller_specific_js) %>
Helper method:
def asset_exist?(path)
if Rails.configuration.assets.compile
Rails.application.precompiled_assets.include? path
else
Rails.application.assets_manifest.assets[path].present?
end
end
The assets are in admin/popular_schools that are admin/popular_schools.js.coffee
<%= controller_specific_js = "#{params[:controller]}.js" %>
The above line outputs: admin/popular_schools.js and adds it.
URL is: http://localhost:3000/admin/popular_schools
assets.rb:
Rails.application.config.assets.precompile += %w( admin/popular_schools.js.coffee )
production.rb
config.assets.compile = true
How exactly I can run it without specifying .js in "#{params[:controller]}.js" as conventially its is not good.
If I remove .js it does not insert it in assets but if I include it it starts to appear.
Also if I use below code:
<%= controller_specific_js = "#{params[:controller]}" %>
<%= javascript_include_tag controller_specific_js if asset_exist?(controller_specific_js) %>
It does work without specifying .js but if I do not want to include any controllers in assets.rb file they start to output exception that file is not included in the assets.erb but it does included in assets folder and for some reasons I do not want to include them in assets.rb
Any workaround or conventions?
I'm using Rails 3.0. How I can include a CSS that I put in /public/stylesheets into a View file in /app/views/posts?
Asset pipeline was introduced in Rails 3.1.
In the previous versions (including Rails 3.0.0), you can link to CSS files with stylesheet_link_tag.
<%= stylesheet_link_tag "main" %>
Including the above code in the view file includes public/stylesheets/main.css file.
More details at http://guides.rubyonrails.org/v3.0.0/layouts_and_rendering.html#linking-to-css-files-with-stylesheet_link_tag
You should put it in /public/assets/ (it's the convention) and then you can do this:
<%= stylesheet_link_tag "example" %>
Or you can put it inside public/assets/stylesheets/ and do:
<%= stylesheet_link_tag "stylesheets/example" %>
I started a new Rails 3.2.5 projects and the asset pipeline is not working anymore. CSS and Javascript file are not compiling anymore.
This is the output from the logs when try to generate the asset:
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-06-16 23:59:11 -0700
Served asset /application.css - 200 OK (0ms)
[2012-06-16 23:59:11] ERROR NoMethodError: undefined method `each' for nil:NilClass
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.3.6/lib/rack/handler/webrick.rb:71:in `service'
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2012-06-16 23:59:11 -0700
Served asset /application.js - 200 OK (0ms)
[2012-06-16 23:59:11] ERROR NoMethodError: undefined method `each' for nil:NilClass
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.3.6/lib/rack/handler/webrick.rb:71:in `service'
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/greg/.rbenv/versions/1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
183:in `block in start_thread'
Update:
<!DOCTYPE html>
<html>
<head>
<title>Shorai</title>
<%= csrf_meta_tags %>
</head>
<body id=<%= params[:controller].sub('_controller', '') %>>
<% if current_user %>
<%= current_user.name %>
<%= link_to "Log out", signout_path %>
<% else %>
<%= link_to "Sign in", "/auth/37signals" %>
<% end %>
<%= yield %>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
</body>
</html>
Update2:
application.scss
*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require_tree .
*/
update3:
http://f.imgtmp.com/Onpqv.png
I don't know what is causing this, anyone has an idea?
Greg
I got this error when I had caching enabled (in development mode) with memcached storage, but the memcached process was not running (Rails 3.2.8, Win7).
The solution therefore was to simply start memcached and reboot the Rails server.
I had a similar problem after activating memcache as cache store (config.cache_store = :dalli_store). I could solve it by deactivating Rack::Cache middleware explicitly (config.action_dispatch.rack_cache = nil), or by setting
config.action_dispatch.rack_cache = {
:metastore => Dalli::Client.new,
:entitystore => 'file:tmp/cache/rack/body',
:allow_reload => false
}
See https://github.com/rails/rails/issues/8366.
I had a similar problem and I solved it by disabling cache in dev environment.
I think cache doesn't play well with asset on-the-fly compilation
I had a similar problem with -v 3.2.5.
After hours of debugging, I put in a workaround. It's not great but it gets me moving until I can solve it, or someone else has better luck!
I'm curious if this workaround works for you too, Gregory...
In config/application.rb I had to explicitly add the load paths for my assets... so:
config.assets.paths << Rails.root.join("app", "assets", "stylesheets")
config.assets.paths << Rails.root.join("app", "assets", "javascripts")
config.assets.paths << Rails.root.join("vendor", "assets", "stylesheets")
config.assets.paths << Rails.root.join("vendor", "assets", "javascripts")
config.assets.paths << Rails.root.join("lib", "assets", "javascripts")
If you have assets in engines, they also need to be explicitly added. It worked OK in -v 3.1.x. The fact that this workaround fixes the issue seems to point to either a bug (surely others would've found it by now) or else some configuration change in 3.2.5.
I'm using the asset pipeline in Rails 3.2.1
My app can having different CSS themes depending on users preferences. but 90% of the stylesheet is the same.
I'd like to have all my CSS compiled in a single file, but with the resulting filename depending on the user's name.
In Rails 2.x I could do this :
<%= stylesheet_link_tag 'main.css', "themes/#{#user.css_theme}.css", :cache => #user.name %>
and the file would have the correct file name.
Now, with Rails 3.1+ I can use ERB to customize the required stylesheets, but if I set this in my layout :
<%= stylesheet_link_tag "application" %>
the compiled file is named application.css and I can't find a way to set a custom name.
You just need define 2 lines in your head :
<%= stylesheet_link_tag "application" %>
<%= stylesheet_link_tag "themes/#{#user.css_theme}.css" %>
And in your application.css you have only the common part.
I'm using Rails 3.1 RC. I wanted to load a CSS manually and not via the asset pipeline.
I've tried it like this and with a hand coded tag:
<%= stylesheet_link_tag "application" %>
<%= stylesheet_link_tag "/stylesheets/global" %>
For some reason FireBug shows me multiple get requests for the global.css file:
Rails bug? Me being stupid?
If anyone is wondering - I use CSSEdit a lot so couldnt work with the file inside the asset pipeline.
did you set the assets.enabled to false in config/application.rb?
# Enable the asset pipeline
config.assets.enabled = false
I would guess this is caused by the same problem as described in Rails 3.1 with Asset Pipeline, link_to :confirm message showing twice?.
You have precompiled assets in your development environment and <%= stylesheet_link_tag "application" %> will expand into multiple tags including each CSS file, one of them being global.css.