DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

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)