Thursday, 3 March 2011

submit an ajax form in javascript winth Rails3 and Prototype

So there is a Rails 3 problem, when you want to submit a form in javascript (without a submit button), it is working on normal way, not ajax. But we can do creating a hidden submit button and then initializing a click event on it what is exactly doing ajax submission. The submit(); is not working anymore because it doesn't call the rails.js functions. It just works when you simple want to create a not ajax post without any confirmation. Let's see an ajax example in view's index.html.erb file:




<%= javascript_tag <<-RUBY
function procedure_init()
{
var submit_button="procedure_submit_button";
var form_name="some_procedure";
// here you can build the form, or modify form parameters
$(submit_button).click();
}
RUBY
%>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%= form_tag "some_url",
:remote => true,
:method => :post,
:name => "some_procedure",
:id => "some_procedure" %>
<%= submit_tag 'procedure_submit_button', :id =>"procedure_submit_button" , :style => "display: none" %>
You can add here your html code for form and create some element what is doing the submission inside a table, for a td:
"javascript: procedure_init();" style="cursor: pointer;">
put here an image for example


That's all. Questions?

No comments:

Post a Comment