Adding page-specific CSS to Rails Asset Pipeline

后端 未结 5 1395
走了就别回头了
走了就别回头了 2020-12-23 22:52

This is a question that several people have asked before, but none of the questions were quite asked or answered in a way that I found helpful, so I\'m writing the quest

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 23:03

    There's one specific action that I want to have use different CSS.

    Here's an alternative way to accomplish what you're looking for:

    Add the controller name and action name to the app body in your /views/layouts/application.html.rb:

    
      <%= yield %>
    
    

    Then in your .scss file:

    .controller_name-action_name {
      // your css goes here
    }
    

    So if your controller was static_pages and your action was home:

    .static_pages-home {
      // your css goes here
    }
    

    Tada! Your css only appears for that specific action.

提交回复
热议问题