insert javascript using link_to_function

I am using Rails 2.3.4 at the moment, and as is well known, rails uses Prototype as default javascript library. For most purposes this suits me well. But what i really like about jQuery is the wealth of plugins readily available. My solution was not something like jRails, but i use them side by side. This might not be the best solution :) First of, you have to include the correct javascript files. Then, at the top of my application.rhtml, i write this: [javascript] var $j = jQuery.noConflict() $j(document).ready(function() { /* insert some jquery magic here */ ... }); [/javascript] This ensures that i can use the jQuery library nicely alongside the Prototype one. Just replace $ by $j for all jQuery calls. For most jQuery things this is more than enough. But then I needed form validation, and needed to be able to call some jQuery from inside a link_to_function block. It took me a while to find, but actually (isn't it always) it is dead-simple. [ruby] def add_document_link(divname, parentdoc ) link_to_function get_icon('adddocument'), { :title => t('adddocument')} do |page| new_doc = Document.new new_doc.doc_ident_p = parentdoc.doc_ident page.select('.add-items-link').each { |b| b.hide } page.insert_html :bottom, divname, :partial => 'documents/add_document', :object => new_doc, :locals => { :divname => "add-items-link"} page << "var $j = jQuery.noConflict(); $j(\"form\").validate();" end end [/ruby] Easy as pie :) I love rails :)
Comments
Add comment

Recent comments

Tags