Leo
Leo

Reputation: 2103

css file specific for a controller

So in my routes.rb file I added this little thingy:

namespace :admin do
    get :dashboard, to: 'dashboard#index'

Now, I do have Admin::DashboardController in my controllers directory, and I added dashboard.css.less into stylesheets/admin. However this file simply isn't loaded when I go to admin/dashboard. What should I do to make this file automatically included? Usually with rails my impression was that proper files are used for controllers without any settings required.

Upvotes: 2

Views: 625

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

You'd be better using the following:

#config/routes.rb
namespace :admin do
  resources :dashboard, only: :index #-> url.com/admin/dashboard
end

Assets

Assets are loaded through manifest files.

Whenever you compile a "manifest file", the require directives tell sprockets to concatenate various other files into that file; intention being to include one file in your layout (rather than 3 separate files).

By default, only application is supported:

# app/assets/stylesheets/application.css
/*
  *= require "x"
*/

If you want to add extra manifest files, you need to add the file to the precompilation queue, and your layout:

# config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( admin/dashboard.js admin/dashboard.css )

# app/assets/stylseheets/application.css
/*
  *= require_tree .
  *= stub admin/dashboard
*/

# app/assets/stylesheets/admin/dashboard.css
/*
  *= require "y"
*/

# app/views/layouts/admin.html.erb
<%= stylesheet_link_tag "admin/dashboard" %>

Upvotes: 2

Related Questions