Assign CSS class to Link to Remote

I’m trying to get link_to_remote to assign a CSS class to the link it
generates. My code is as follows:

<%=
link_to_remote “Show Full Info”,
:update => “fullcontact” + reparray.last.to_s,
:url => “/cm/full_contact/” + reparray.last.to_s,
:classname => “contactlink”
%>

Which produces this HTML:

Show Full Info

And I would like it to produce this HTML:

Show Full Info

I’ve also tried the code this way:

<%=
link_to_remote “Show Full Info”,
{ :update => “fullcontact” + reparray.last.to_s,
:url => “/cm/full_contact/” + reparray.last.to_s},
{ :classname => “contactlink” }
%>

(The only difference is the addition of the {}'s.) The result is the
same.
What am I doing wrong here? I don’t want to statically write the HTML
into
the view, which would work fine, but just isn’t as elegant as Ruby on
Rails
should be.

Thanks,
David

Thanks for the help, Daniel. The code you provided produced an error,
(wrong number of arguments, 4 for 3), but it was your explanation that
helped me the most. The correct code to do what I was looking for was
actually:

<%=
link_to_remote “Show Full Info”,
{ :update => “fullcontact” + reparray.last.to_s,
:url => “/cm/full_contact/” + reparray.last.to_s},
{ :class => “contactlink” }
%>

The empty hash you had in the middle of the code was causing the error.
Thanks for the help, again, Daniel, and I hope this helps anybody else
who
has this problem!

  • David

If you go to the API page,
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M
000412, you’ll see that the signature for link_to_remote is

link_to_remote(name, options = {}, html_options = {})

So, the secong argument is an options hash, and the last is the
html_options
hash - html_options is where you’d set the class. Your code should look
like

link_to_remote “Show Full Info”,
{ :update => “fullcontact” + reparray.last.to_s,
:url => “/cm/full_contact/” + reparray.last.to_s},

{},
{ :class => “contactlink” }

Also notice that it’s :class, not :classname.

Hope this helps!

Daniel


From: [email protected]
[mailto:[email protected]] On Behalf Of David R.
Sent: Wednesday, June 28, 2006 8:58 AM
To: [email protected]
Subject: [Rails] Assign CSS class to Link to Remote

I’m trying to get link_to_remote to assign a CSS class to the link it
generates. My code is as follows:

<%=
link_to_remote “Show Full Info”,
:update => “fullcontact” + reparray.last.to_s ,
:url => “/cm/full_contact/” + reparray.last.to_s,
:classname => “contactlink”
%>

Which produces this HTML:

<a
href="#" onclick=“new Ajax.Updater(‘fullcontact7’,
‘/cm/full_contact/7’,
{asynchronous:true, evalScripts:true}); return false;”

Show Full Info

And I would like it to produce this HTML:

<a
href="#" class=“contactlink” onclick=“new Ajax.Updater(‘fullcontact7’,
‘/cm/full_contact/7’, {asynchronous:true, evalScripts:true}); return
false;”

Show Full Info

I’ve also tried the code this way:

<%=
link_to_remote “Show Full Info”,
{ :update => “fullcontact” + reparray.last.to_s,
:url => “/cm/full_contact/” + reparray.last.to_s},
{ :classname => “contactlink” }
%>

(The only difference is the addition of the {}'s.) The result is the
same.
What am I doing wrong here? I don’t want to statically write the HTML
into
the view, which would work fine, but just isn’t as elegant as Ruby on
Rails
should be.

Thanks,
David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs