Anthony Ramm
2006-Mar-22 19:48 UTC
[Rails] Changing the onsubmit event using an RJS template
Hi there, I''m currently writing my first ROR application, and must say I''m loving the framework. Just a quickie, which people will probably instantly know the answer to. I''ve got a select box which I want to be able to add values to on the fly using AJAX. I''ve got the form all appearing and updating the database, however, what I want to be able to do is change the onsubmit event for the form so if someone presses enter whilst entering a new value for the select box the application will save the new value for the select box rather than submitting the form to the original action. I''ve worked out that the javascript I need can be generated with this:- options = { :url => {:action => ''ajax_add''}, :with => "Form.Element.serialize(''idea_type_title'')" } page << "$(''edit_idea_form'').onsubmit = function() { # { remote_function(options) }; return false; }" However... This seems a bit ugly to me. It seems that the ruby way would be to be able to use one of the helper functions to do this. I''d like to be able to do something like: page[''edit_idea_form''].onsubmit = "function() { #{ remote_function (options) }; return false; }" But this insists on generating (with the quotes, which unfortunately doesn''t work): $("edit_idea_form").onsubmit = "function() { new Ajax.Request(''/ idea_types/ajax_add'', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(''idea_type_title'')}); return false; }"; rather than: $("edit_idea_form").onsubmit = function() { new Ajax.Request(''/ idea_types/ajax_add'', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(''idea_type_title'')}); return false; } even this seems a little clunky. Any ideas on simplifications that could be made to the code would be gratefully received as the chances are it''s going to have to be used multiple times for various select boxes over the site. Much thanks in advance, Anthony ----- Anthony Ramm OpenAdvantage Systems Specialist T: 0121 6341620 F: 0121 6341630 E: anthony@openadvantage.org Be enlightened - you are in control To find out more about our free IT consultancy and to visit a place on a seminar or training workshop visit http://www.openadvantage.org/ events Freedom, Choice, Control -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060322/7747b141/attachment.html