Forum: Ruby on Rails Using onSubmit tag in form_tag?

Fcdafe495dccc04d27f6dbbcdf54aecc?d=identicon&s=25 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!
A50b32c68f4c22076abe38cd2ceb1076?d=identicon&s=25 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.
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 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.
633ada55ad6d9831e08a6ae23270baf4?d=identicon&s=25 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.
760e5c90000057a6860986b69b77c400?d=identicon&s=25 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

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.