开发者

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 wh开发者_如何学Goat 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> 


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜