maletor
maletor

Reputation: 7122

Converting form_remote_tag to form_for for Rails 3 UJS

I'm trying to convert this for Rails 3

<%= form_remote_tag :url => feedback_url, 
  :update    => 'flash_message', 
  :before    => "doSomething()", 
  :condition => "condition()", 
  :complete  => "doSomethingElse();" -%>

Here's what I have so far

<%= form_tag feedback_url, :remote => true, :id => 'form' do %>
  <%# Gather data %>
<% end -%>
<script>
    $(function() { 
      $("#form").bind("ajax:beforeSend", function() {
          doSomething();   
        }).bind("ajax:success", function() {
          doSmomethingElse();
        });
      });   
    });
</script> 

Upvotes: 1

Views: 3585

Answers (2)

utiq
utiq

Reputation: 1381

I found this post for you. I am sure that you will solve https://web.archive.org/web/20201109001755/https://www.alfajango.com/blog/rails-3-remote-links-and-forms/

Upvotes: 2

Kyle d&#39;Oliveira
Kyle d&#39;Oliveira

Reputation: 6382

I like to do this a slightly different way.

<%= form_tag feedback_url, :remote => true, :id => 'feedback_form' do %>
  ...
<% end %>
<script type="text/javascript">
  $("form#feedback_form").submit(function(event){
    $('flash_notice').css('display', 'block');
    return condition() == true
  });
</script>

If the submit handler for the form returns false then it will not submit the form. (I'm pretty sure that is the case, but event.preventDefault() might work if that does not) And this will also take care of the before conditions.

For the complete and update though I find it better to use a different template. For instance, lets say that you form submits to the 'new' action of some controller. You can have a template new.js.erb with

  $('flash_notice').css('display', 'none')
  doSomething();

In this template you also have access to instance variables defined in the controller.

Upvotes: 1

Related Questions