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

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, and nothing seems to work.

What do I need to do to enable external CSS files with a rails application? I’m new to rails, so forgive me if this is basic.

Solution:

Put the CSS files in public/stylesheets and then use: <%= stylesheet_link_tag “filename” %>

How do you manage css stylesheet files and inclusions in a large ruby on rails application?

How should you manage the organizing of css files and inclusions in a large ruby on rails application, assuming you’re not using a framework? Naturally you’d have your application level styles (e.g. r

Do I need to use multiple databases in Ruby on Rails application?

I am designing a CRM using Ruby on Rails. How do you think, do I need a separate database for every client company? Or should I use the same database for everyone?

How do I published a Ruby on Rails application online?

I have a simple Ruby on Rails application that works through a localhost test (both using sqlite, or ruby mysql2 gem). I have a web server ready to upload my app online. I understand that I need to cr

How do I use rails helper in an emberjs application

I want to use rails helper method, for example I want to use form_for or root_url or devise’s sign_in?, how could I do that?

How do I get TextMate to use Ruby on Rails by default?

How do I make TextMate use the Rails bundle by default? On some files it’s enabled, and on others (including models) it’s not, and I wish it would just assume that every .rb file is a Ruby on Rails fi

Ruby On Rails with HTML5 and CSS3 [closed]

Does Ruby On Rails automatically use HTML5 and CSS3? If not how do I put them in my Ruby On Rails website?

How do you deploy a Ruby on Rails application on hostgator?

How do you deploy a Ruby on Rails application on hostgator?

How to improve the way I use Textmate for Ruby on Rails, HTML, CSS and Javascript?

I use the latest version of textmate for all my Rails development needs and I feel that I’m under-using the possibilities or this software. I only installed the Ruby on Rails bundle and use little to

How do we use MongoDB in Ruby on Rails?

Does anybody know how to create a rails application that use noSql database? We do rails new app_name -d mysql for using mysql database, I need to know what command I should fire in case of noSql data

How do I use CSS in Django?

I am creating my application using Django, and am wondering how I can make Django use my CSS file? What settings do I need to do to make Django see the css file? NB: On a local machine

Answers

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.

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.

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

In the latest version of rails, you can do 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.

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

app/asset/stylesheets

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

read more here about the asset pipeline

The original post might have been true back in 2009, but now it is actually incorrect now, and no linking is even required for the stylesheet as I see mentioned in some of the other responses. Rails will now do this for you by default.

  • Place any new sheet .css (or other) in app/assets/stylesheets
  • Test your server with rails-root/scripts/rails server and you’ll see the link is added by rails itself.

You can test this with a path in your browser like testserverpath:3000/assets/filename_to_test.css?body=1

Use the rails style sheet tag to link your main.css like this

<%= stylesheet_link_tag "main" %>

Go to

config/initializers/assets.rb

Once inside the assets.rb add the following code snippet just below the Rails.application.config.assets.version = ‘1.0’

Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( main.css )

Restart your server.