Additional link tag attributes with link_to_function


#1

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


#2

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


#3

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