Forum: Ruby on Rails Using onSubmit tag in form_tag?

Posted by Woei Shyang (generik)
on 2006-03-21 03:44
Hi guys,

I have been working on a form that would use javascript to validate the 
data before submitting it, and from my view.rhtml I have a statement 
like this:



However such a statement generates a HTML tag that looks like this:

<form action="/users/Login?onSubmit=validate%28%29" method="post">

Seems like Rails thinks it is a parameter instead of an option, so what 
I tried next was something like:

<%= start_form_tag {:action => 'Add'}, {:onSubmit => 'validate()'} %>

Sadly that doesn't compile. From rubydocs the syntax for form_tag is 
form_tag(url_for_options = {}, options = {}, *parameters_for_url), so 
how do I specify the options that I'd want as in this case?

Thanks!
Posted by Marshall Roch (mroch)
on 2006-06-19 09:46
Woei Shyang wrote:
> <%= start_form_tag {:action => 'Add'}, {:onSubmit => 'validate()'} %>
> 
> Sadly that doesn't compile. From rubydocs the syntax for form_tag is 
> form_tag(url_for_options = {}, options = {}, *parameters_for_url), so 
> how do I specify the options that I'd want as in this case?

Just had the same problem.  You need to use parentheses around all of 
the arguments:

<%= start_form_tag( {:action => 'Add'}, {:onSubmit => 'validate()'} ) %>

--
Marshall Roch

P.S. I realize this is old, but I found it so I figured I'd pass it on 
for others.
Posted by Alex Wayne (Guest)
on 2006-06-19 19:08
Marshall Roch wrote:
> Woei Shyang wrote:
>> <%= start_form_tag {:action => 'Add'}, {:onSubmit => 'validate()'} %>
>> 
>> Sadly that doesn't compile. From rubydocs the syntax for form_tag is 
>> form_tag(url_for_options = {}, options = {}, *parameters_for_url), so 
>> how do I specify the options that I'd want as in this case?
> 
> Just had the same problem.  You need to use parentheses around all of 
> the arguments:
> 
> <%= start_form_tag( {:action => 'Add'}, {:onSubmit => 'validate()'} ) %>
> 
> --
> Marshall Roch
> 
> P.S. I realize this is old, but I found it so I figured I'd pass it on 
> for others.

Correct.  The problem is ruby has a hard time distinguishing between 
passed hashes, and associated blocks tacked onto method calls.  Using 
parens fixes the issue since hashes would be isnside the parens, and 
blocks would be outside.
Posted by mixplate (Guest)
on 2007-04-22 20:26


hi when i do this....

<%= start_form_tag( {:action => 'Add'}, {:onSubmit => :confirm=>'are you 
sure'} ) %>



the popup appears but both the ok and cancel buttons, no matter which 
you you select, submits the form...

how can i make it so that if the user selects cancel or ok, that the 
form does not get submit and enables the user to recheck the 
information?





Alex Wayne wrote:
> Marshall Roch wrote:
>> Woei Shyang wrote:
>>> <%= start_form_tag {:action => 'Add'}, {:onSubmit => 'validate()'} %>
>>> 
>>> Sadly that doesn't compile. From rubydocs the syntax for form_tag is 
>>> form_tag(url_for_options = {}, options = {}, *parameters_for_url), so 
>>> how do I specify the options that I'd want as in this case?
>> 
>> Just had the same problem.  You need to use parentheses around all of 
>> the arguments:
>> 
>> <%= start_form_tag( {:action => 'Add'}, {:onSubmit => 'validate()'} ) %>
>> 
>> --
>> Marshall Roch
>> 
>> P.S. I realize this is old, but I found it so I figured I'd pass it on 
>> for others.
> 
> Correct.  The problem is ruby has a hard time distinguishing between 
> passed hashes, and associated blocks tacked onto method calls.  Using 
> parens fixes the issue since hashes would be isnside the parens, and 
> blocks would be outside.
Posted by John Griffiths (indiehead)
on 2008-02-26 10:18
had a hunt around, seems you might need a 'return' for your validation.

something like...

<%= form_tag( {:action => 'Add'}, {:onSubmit => 'return validate();'} ) 
%>

then in your validate(); javascript function you can put the validation 
code, returning true if ok, and false if not.

Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.