Ruby On Rails Lesson
It’s time now to revisit the line from Listing 4.1 used in the layout to include the cascading style sheets:
We are now nearly in a position to understand this. As mentioned briefly in Section 4.1, Rails defines a special function to include stylesheets, and
is a call to this function. But there are two mysteries. First, where are the parentheses? In Ruby, they are optional; these two lines are equivalent:
Second, the :media argument sure looks like a hash, but where are the curly braces? When hashes are the last argument in a function call, the curly braces are optional; these two lines are equivalent:
So, we see now that the line
calls the stylesheet_link_tag function with two arguments: a string, indicating the path to the stylesheet, and a hash, indicating the media type. Because of the <%= %> brackets, the results are inserted into the template by ERb, and if you view the source of the page in your browser you should see the HTML needed to include a stylesheet (Listing 4.7). (You may see some extra things, like ?body=1, after the CSS filenames. These are inserted by Rails to ensure that browsers reload the CSS when it changes on the server.)
Listing 4.7 The HTML source produced by the CSS includes
<link href= "/assets/application.css" media= "all" rel= "stylesheet"
type= "text/css" />
If you actually view the CSS file by navigating to http://localhost:3000/assets/ application.css, you’ll see that (apart from some comments) it is empty. We’ll set
about changing this in Chapter 5.