Adding :class parameter to link_to_remote in Rails
Well, this is mostly a note to myself, since I have spend almost half an hour trying to figure how to add class parameter to a link generated by link_to_remote in Rails.
Well here is the code (with all bells and whilstles):
<%= link_to_remote "Vote on it",
{
:url => { :action => 'vote', :id => post_dig.id },
:update => 'thevote',
:before => visual_effect(:opacity, 'thevote', :duration => 0.2, :from => 1.0, :to => 0.1),
:complete => visual_effect(:opacity, 'thevote', :duration => 0.5, :from => 0.1, :to => 1.0)
},
:class => "vote"
%>
This code updates the contents of an element with id='thevote', and adds nice fade-out/in during the update. Most important that it also adds class "vote" to the generated link so it can be used for a nice CSS rollover.
November 27th, 2006 at 5:59 pm
Well, in my Firefox I have "link so it can be used for a nice CSS rollover." that points to "http://macdiggs.com". And I don't have visual effects on it 8\
What have to be ?
November 27th, 2006 at 6:38 pm
looks like problems with posting :) fixed thanks :)
November 27th, 2006 at 9:16 pm
Oh yeah, I forgot there are nice fadey effects in RoR. Is it some kind of add-on?
I really need to make some time to start learning it. At the moment I'm finishing an OO PHP-based site for University coursework and it's already unmanageable. I'm thinking about rewriting it in Ruby on Rails once I get some time. I'm sure it'll be mind-blowingly small and easy to read.
Did you read any good books or tutorials when you learnt, Mike? Or, did you learn Ruby first and then learn Rails?
November 28th, 2006 at 6:26 pm
Well basically, it's pretty hard to understand Rails without reading anything or reading any kind of tutorials, IMO (not impossible but just a big waste of time).
I would recommend starting with reading "Programming Ruby" and "Agile Web Development with Rails" from Pragmatic Programmer. Don't know about an order - it might be just find to read the both books at the same time, having the "Programming Ruby" as a language manual handbook since there are some pretty interesting stuff in Ruby itself.
Once you have read the stuff once, I would also recommend getting "Receipts on Rails" from the same Pragmatic folks. The book contains lost of interesting bits on using Rails in the wild.
Alas, the stuff doesn't cover all the Rails to the degree I would like it to, but books are really-really good anyway.
Heh and heah, the fadey effects are a part of Rails integration with the Prototype/Scriptaculous library - really easy to use.
December 11th, 2006 at 6:56 pm
Thank you so much for posting how to display the css class in a link_to_remote. Trying different combinations of curly braces and hashes was getting me nowhere…I never would have thought to place all "options" and all "html_options" (as described in the RoR framework documentation) in separate hashes…the documentation now makes sense! Thanks again!
May 22nd, 2007 at 5:21 pm
A good explanation from another blog (http://dev.rousette.org.uk/ticket/34):
"You can add class links to link_to_remote — you just have to bind the hashes to the proper argument. The args are name, options = {}, html_options = {}, which makes options greedy, even at the expense of starving html_options. The solution is to place explicit braces around your options argument.
Here's an example of a link_to_remote call that assigns the "nav_item" class to the a href: link_to_remote(@file.caption, { :update => 'media', :url => { :action => 'ajax_item', :id => @file, :shortcut => @review.shortcut } }, :class => 'nav_item')"