# Additional link tag attributes with link\_to\_function

**URL:** <https://www.ruby-forum.com/t/additional-link-tag-attributes-with-link-to-function/55271>\
**Category:** Rails\
**Created:** [March 18, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/additional-link-tag-attributes-with-link-to-function/55271 "2006-03-18T21:05:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [March 18, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/additional-link-tag-attributes-with-link-to-function/55271/1 "2006-03-18T21:05:46Z")

</div>

Hi,

I’m having a bit of a struggle with link\_to\_remote. Everything works  
exactly as I want it, but the problem is that I want to add another  
attribute to the -tag, namely class="". Long story short, I want  
specific links to look differently from others, but all of them being  
link\_to\_function. Normally, I’d do this with adding a class=“foo” to  
certain links and specifying the look with css, but I’m unsure how to do  
this with Rails.

Currently, I have:

\<%= link\_to\_remote(“foo”,  
:update =\> ‘some-id’,  
:url =\> { :action =\> :someaction, :params =\> { :parameter =\>  
“value” }},  
:loading =\> “Element.hide(‘some-id’)”,  
:complete =\> visual\_effect(:appear, ‘some-id’)) %\>

which generates

foo

, just as it should. Basically, I wish to extend this to:

foo

, but have no idea how to add the class-attribute. I’m _guessing_ this  
might be related to html\_options = {} in the link\_to\_function method,  
but documentation is non-existant.

Of course, I _could_ work around this by using:

bar

and then use

.class a{ stuff; }

in my css, but I don’t want to do that if I can help it.

Is there a way?

Thanks in advance,

Sebastian

---

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [March 18, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/additional-link-tag-attributes-with-link-to-function/55271/2 "2006-03-18T21:33:19Z")

</div>

On Mar 18, 2006, at 21:05, Sebastian Conrad wrote:

> I’m having a bit of a struggle with link\_to\_remote. Everything works  
> exactly as I want it, but the problem is that I want to add another  
> attribute to the -tag, namely class="". Long story short, I want  
> specific links to look differently from others, but all of them being  
> link\_to\_function. Normally, I’d do this with adding a class=“foo” to  
> certain links and specifying the look with css, but I’m unsure how  
> to do  
> this with Rails.

As you correctly guessed this has to do with html\_options.

The signature of link\_to\_remote is

link\_to\_remote(name, options = {}, html\_options = {})

and in Rails is important to understand what that implies because it  
is used often, this comes from Ruby itself.

That method receives 3 parameters, two of them, the rightmost ones,  
have default values. In the case of hashes Ruby does a bit of  
syntactic sugar to dispense the curlies, so:

link\_to\_remote(‘foo’, :a =\> 3, :b =\> 1)

is equivalent to

link\_to\_remote(‘foo’, {:a =\> 3, :b =\> 1}, {})

so trailing pairs are magically converted into the second positional  
hash parameter. That’s Ruby, not Rails.

Now, to be able to pass options to html\_options you need to make  
“options” explicitly as a true hash:

link\_to\_remote(‘foo’, {:a =\> 3, :b =\> 1}, :class =\> ‘my\_css\_class’)

That way trailing pais are grouped as html\_options, and you get the  
class attribute generated by the helper.

– fxn

---

<div class="post-metadata">

**Author:** ![chidori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/edb3f5/32.png) [@chidori](https://www.ruby-forum.com/u/chidori)\
**Post date:** [March 19, 2006, 12:07am UTC](https://www.ruby-forum.com/t/additional-link-tag-attributes-with-link-to-function/55271/3 "2006-03-19T00:07:12Z")

</div>

Xavier N. wrote:

> link\_to\_remote(‘foo’, :a =\> 3, :b =\> 1)
> 
> is equivalent to
> 
> link\_to\_remote(‘foo’, {:a =\> 3, :b =\> 1}, {})
> 
> so trailing pairs are magically converted into the second positional  
> hash parameter. That’s Ruby, not Rails.
> 
> Now, to be able to pass options to html\_options you need to make  
> “options” explicitly as a true hash:
> 
> link\_to\_remote(‘foo’, {:a =\> 3, :b =\> 1}, :class =\> ‘my\_css\_class’)

Awesome, thanks. I knew the answer would be simple, but it escaped me  
somehow. Thanks again!

- Sebastian
