简体   繁体   English

在Rails中选择框的更改事件触发后,如何使用jQuery UJS通过AJAX提交表单?

[英]How can I use jQuery UJS to submit a form via AJAX after the change event of a select box fires in Rails?

How can I use jQuery UJS to submit a form after the change event of a select box fires in Rails? 在Rails中选择框的更改事件触发后,如何使用jQuery UJS提交表单?

My view looks like this: 我的观点如下:

<% for perm in @permissions %>
  <%= form_for [@brand,perm], { :remote => true } do |f| %>
    <tr id="permission_<%= perm.id %>">
      <td><%= perm.configurable.name %></td>
      <td><%= f.select :action, ['none', 'read', 'update', 'manage'] %></td>
    </tr>
  <% end %>
<% end %>

Pretty straightforward. 非常直截了当。 My controller is like so: 我的控制器是这样的:

class PermissionsController < ApplicationController

    before_filter :authenticate_user!

    respond_to :html, :js

    load_and_authorize_resource :brand
    load_and_authorize_resource :permission, :through => :brand

    def index
    end

    def update
      @permission = Permission.find(params[:id])
      @permission.update_attributes(params[:permission])
    end

end

And in my application.js: 在我的application.js中:

// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
  $("#permission_action").change(function() {
    this.form.submit(); // This needs to be an AJAX call, but how?
  });
})

Please note that the form submission fires just fine. 请注意,表单提交正常。 But it is doing a regular post rather than an AJAX submission. 但它正在做一个常规的帖子,而不是一个AJAX提交。 When I place a submit button on the form and use that, the AJAX submission works fine. 当我在表单上放置一个提交按钮并使用它时,AJAX提交工作正常。 I need to change: 我需要改变:

this.form.submit();

to an AJAX call, but I don't know how. 一个AJAX电话,但我不知道如何。 Can anyone help? 有人可以帮忙吗?

Looking through the jquery-ujs sources, i figured that this might be the safest way: 通过jquery-ujs来源,我认为这可能是最安全的方式:

// in application.js
jQuery.fn.ajaxSubmit = function() {
    this.trigger('submit.rails');
};

it just fires the event as would .submit() but in a ujs flavour, so you would call 它只是像.submit()那样触发事件,但是在ujs风格中,所以你会打电话

$('.my_form').ajaxSubmit();

I see you are already using jQuery that's good. 我看到你已经在使用jQuery了。 Following code was taken from here: http://railscasts.com/episodes/136-jquery 以下代码取自此处: http//railscasts.com/episodes/136-jquery

I strongly advise you get familiar with these screen casts they help a ton (understatement). 我强烈建议你熟悉这些屏幕演员他们帮助吨(低调)。

// public/javascripts/application.js
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};

$(document).ready(function() {
  $("#new_review").submitWithAjax();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM