I have following piece of code. Here is the original post
Conditionally setting CSS style from ruby controller
%th{:class => @title_header}= link_to 'Title', my_path(:sort => 'title'), :id => 'title_header'
I know that the link_to work
1.I dont understand what is :class and is :class a keyword?
2.I understand that when we create a model and we will get a model_path,
but in this case why we can pass an argument into my_path?
3.Is that :sort also a keyword in rails?
4.is :id a keyword in rails?
5.so in this case , what we doing with my_path? is it going to return something?
6.how about the :id, what it does in this case??
Thankyou!
:sort and :class are symbols in Ruby, representing identifiers that don’t change in code the way a string can. They are being used as the key in a hash definition of keys/values.
When you pass a hash of name/value pairs to a tag like %th in HAML, it convents them into HTML attributes on the tag. This allows you to dynamically set the value of the “class” attribute on the TH tag with a list of class names from the instance variable @title_header.
my_path is a named route helper that represents a function to generate the URI for a route named in the Rails routeset. You can pass a variety of options to change how the URI is generated. By default, any option that isn’t a route generation option gets injected as a query parameter on the URI generated. So if my_path => /my/path, then my_path(:foo => ‘bar’) => /my/path?foo=bar.
In this case, this query parameter likely dictates the sort order of the data generated at that URI.
In the same way, the link_to function takes an option hash as it’s last argument that determines how the link element is generated. The last option set on this function is the html_options, where the attributes of the hash map to attributes of the A tag generated. In this case, the :id symbol and value in the hash will generate an “id” HTML attribute with the given value.
Since you asked, here’s a good reference on what a CSS class is: http://www.tizag.com/cssT/class.php