DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

3 2

load stylesheet from controller_path

1. Call a metaprogramming code from the ApplicationLayout as the root.

# app/views/layouts/application.html.erb
<%= load_stylesheet_from_controller_path %>

2. Evaluate controller path, and return the stylesheet_link_tag of the path.

# app/helpers/application_helper.rb
def load_stylesheet_from_controller_path
  return unless File.exist?("#{Rails.root}/app/assets/stylesheets/#{controller.controller_path}.css.scss")

  stylesheet_link_tag stylesheet_path(controller.controller_path)
end

you can use controller.action_name instead of the path method.


πŸ”— Parent Note

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’