How do I use CSS with a ruby on rails application?

后端 未结 8 458
情书的邮戳
情书的邮戳 2020-12-01 04:00

How do I use CSS with RoR? When I link externally, I\'m never able to see the files. I cp\'d the .css file to every folder I could think of...views, controller, template, an

相关标签:
8条回答
  • 2020-12-01 04:32

    Put the CSS files in public/stylesheets and then use:

    <%= stylesheet_link_tag "filename" %>
    

    to link to the stylesheet in your layouts or erb files in your views.

    Similarly you put images in public/images and javascript files in public/javascripts.

    0 讨论(0)
  • 2020-12-01 04:35

    Have you tried putting it in your public folder? Whenever I have images or the like that I need to reference externally, I put it all there.

    0 讨论(0)
  • 2020-12-01 04:37

    If you are using rails > 3 version, then there is a concept called asset pipeline. You could add your CSS to

    app/assets/stylesheets
    

    then it will automatically be picked up by the app. (this is useful as rails will automatically compress the CSS files)

    read more here about the asset pipeline

    0 讨论(0)
  • 2020-12-01 04:43

    I did the following...

    1. place your css file in the app/assets/stylesheets folder.
    2. Add the stylesheet link <%= stylesheet_link_tag "filename" %> in your default layouts file (most likely application.html.erb)

    I recommend this over using your public folder. You can also reference the stylesheet inline, such as in your index page.

    0 讨论(0)
  • 2020-12-01 04:51

    With Rails 6.0.0, create your "stylesheet.css" stylesheet at app/assets/stylesheets.

    0 讨论(0)
  • 2020-12-01 04:52

    To add to the above, the most obvious place to add stylesheet_link_tag is in your global application layout - application.html.erb.

    0 讨论(0)
提交回复
热议问题