Forum: Ruby on Rails Changing the onsubmit event using an RJS template

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91a5a39129309428a6f634e7551c1314?d=identicon&s=25 Anthony Ramm (Guest)
on 2006-03-22 20:48
(Received via mailing list)
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
This topic is locked and can not be replied to.