Ruby On Rails Classroom image

Neha  Jaggi / Professional / Web Technology

To post your Question Join Classroom
Lesson Topics's No:-  ||
Lessons:-CSS revisited

CSS revisited

It’s time now to revisit the line from Listing 4.1 used in the layout to include the cascading style sheets:

<%=   stylesheet link tag  "application",  :media =>  "all" %>

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

stylesheet link tag   "application",   :media =>   "all"  

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:

# Parentheses on function calls are optional.
stylesheet link tag   ("application",  :media =>   "all")
stylesheet link tag   "application",   :media =>  "all"

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:  

# Curly braces on final hash arguments are optional.
stylesheet link tag   "application",   { :media =>  "all" }
stylesheet link tag   "application",    :media =>   "all"

So, we see now that the line

stylesheet link tag  "application":media =>  "all"

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.

Neha  Jaggi

Skills    Ruby On Rails

Qualifications :- High School - , College/University - Graphic Era Hill University, Dehradun, College/University - ,
Location :-Dehradun,Dehradun,UTTARAKHAND,India

Experienced Software Developer with a demonstrated history of working in the Information Technology and services industry. Skilled in Web Technologies (Ruby on Rails, PostgreSQL, php, Laravel and AJAX). 


  Students (0)