Forum: Ruby on Rails Additional link tag attributes with link_to_function...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
35544c54308ea4e0cf62e28a9782afc7?d=identicon&s=25 Sebastian Conrad (chidori)
on 2006-03-18 21:05
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 <a>-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

<a href="#" onclick="new Ajax.Updater('some-id',
'/controller/someaction?parameter=value', {asynchronous:true,
evalScripts:true, onComplete:function(request){new
Effect.Appear('some-id',{});},
onLoading:function(request){Element.hide('some-id')}}); return
false;">foo</a>

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

<a href="#" class="bar" onclick="new Ajax.Updater('some-id',
'/controller/someaction?parameter=value', {asynchronous:true,
evalScripts:true, onComplete:function(request){new
Effect.Appear('some-id',{});},
onLoading:function(request){Element.hide('some-id')}}); return
false;">foo</a>

, 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:

<span class="foo"><a href="#">bar</a></span>

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
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-03-18 21:33
(Received via mailing list)
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 <a>-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
35544c54308ea4e0cf62e28a9782afc7?d=identicon&s=25 Sebastian Conrad (chidori)
on 2006-03-19 00:07
Xavier Noria 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
This topic is locked and can not be replied to.