Sebastian Conrad
2006-Mar-18 20:05 UTC
[Rails] Additional link tag attributes with link_to_function...
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
--
Posted via http://www.ruby-forum.com/.
Xavier Noria
2006-Mar-18 20:32 UTC
[Rails] Additional link tag attributes with link_to_function...
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
Sebastian Conrad
2006-Mar-18 23:07 UTC
[Rails] Re: Additional link tag attributes with link_to_function...
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 -- Posted via http://www.ruby-forum.com/.