Attaching a css class to a link_to method

I’m trying to create links that have classes using link_to. The only
problem is I can’t figure out the syntax from the Rails API. It says
this:

link_to(name, options = {}, html_options = nil,
*parameters_for_method_reference)

But I know that’s not what you really need to type in because I tried:

<%= link_to “Downloads”, :controller => ‘downloads’,
html_options={class=“here”} -%>

and got an error. I even tried putting a colon in front of html_options
to make it look like a symbol like the rest of them. No dice.

I’m pretty sure this just comes back to a fundamental inability to read
the api correctly. I’ve been doing this for a year but I still don’t
know what “options = {}” translates into, for example. Obviously it
isn’t meant to be typed in as it is.

Can anybody explain this for me?

Thanks!

On Aug 31, 9:40 am, Sean C. [email protected]
wrote:

html_options={class=“here”} -%>

Thanks!

Posted viahttp://www.ruby-forum.com/.

Hi Sean,

I think the code you’re after is:

<%= link_to “Downloads”, {:controller => ‘downloads’}, {:class =>
“here”} -%>

Here’s my take on the API’s spec, but I’m fairly new to Ruby / Rails,
so I might be wrong:

link_to takes a name argument. In the example above, this is
“Downloads”.

It also takes a hash of options. If no options are specified, it
defaults to the empty hash, denoted by {}. In the example above,
options is the hash: {:controller => ‘downloads’}

It also takes a second hash of html_options, which defaults to nil, if
no hash is supplied. In the example above, html_options is {:class =>
“here”}

A source of confusion is that if a hash is the last argument to a Ruby
method, the brackets may be omitted. Therefore the following is
equivalent to the example above:

<%= link_to “Downloads”, {:controller => ‘downloads’}, :class =>
“here” -%>

Finally, the following is not equivalent to the first example, and
will result in options containing both controller and class entries;
meanwhile html_options will be nil:

<%= link_to “Downloads”, :controller => ‘downloads’, :class => “here” -
%>

Hope this helps some.

Cheers,
Louis.

Hi Sean,

this works:
<%= link_to “Downloads”, {:controller => ‘downloads’}, :class =>
‘myclass’ -%>

Regards,
Andreas

On 31 Aug., 10:40, Sean C. [email protected]

OK. Let me just rephrase to make sure I get it: the API gives only the
order of arguments, not their syntax? Because their syntax is usually
similar (:something => ‘something’ or {:something => ‘something’}), so
it has simply been omitted?

Anyway, thanks!
sean

On Aug 31, 10:24 am, Sean C. [email protected]
wrote:

OK. Let me just rephrase to make sure I get it: the API gives only the
order of arguments, not their syntax? Because their syntax is usually
similar (:something => ‘something’ or {:something => ‘something’}), so
it has simply been omitted?

Anyway, thanks!
sean

Posted viahttp://www.ruby-forum.com/.

The snippet from the API that you’re looking at is the method
signature. So to answer your question, yes this part of the API only
specifies the order of the arguments (and their names, but that’s not
too important to you.) To get a complete picture of what a method
does, consider also the description of the method.

For the link_to method, that description includes the following text:
"The html_options will accept a hash of html attributes for the link
tag. It also accepts 3 modifiers that specialize the link behavior.

* :confirm => 'question?': This will add a JavaScript confirm

prompt with the question specified. If the user accepts, the link is
processed normally, otherwise no action is taken.
* :popup => true || array of window options: This will force the
link to open in a popup window. By passing true, a default browser
window will be opened with the URL. You can also specify an array of
options that are passed-thru to JavaScripts window.open method.
* :method => symbol of HTTP verb: This modifier will dynamically
create an HTML form and immediately submit the form for processing
using the HTTP verb specified."

This explains precisely what are considered valid contents for the
html_options hash (i.e. the third argument to link_to).

For reference, a complete description of the link_to method, check
out:
http://www.noobkit.com/actionpack-actionview-helpers-urlhelper-link_to

Hope this helps.

Cheers,
Louis.