1

我正在尝试通过 ajax 提交表单并包含一些我通过 jquery 设置的变量。

/views/applications/_form.html.erb

<%= form_for(@application, :html => {:class => "organizer"}, :remote => true) do |f| %>
  // fields in here
<% end %>

/控制器/applications_controller.rb

  # PUT /applications/1
  # PUT /applications/1.json
  def update
    @application = Application.find(params[:id])
    p params[:xposition]     # these get set in jquery
    p params[:application]   # these get set in jquery
    respond_to do |format|
      if @application.update_attributes(params[:application])
        @curr_app = ApplicationField.last

        format.html { redirect_to @application, notice: 'Application was successfully updated.' }
        format.json { head :no_content }
        format.js { render action: "update" }
      else
        format.html { render action: "edit" }
        format.json { render json: @application.errors, status: :unprocessable_entity }
        format.js { render action: "update" }
      end
    end
  end

/assets/javascripts/applications.js

$('.organizer').submit(function() {

        var dataToSubmit = $(this).serialize();
        var field_values = [];
        var x_values = [];
        var y_values = [];

        // i add values to the arrays above here

        for(i = 0; i < field_values.length; ++i) {
            dataToSubmit += "&field_name="+field_values[i]+"&xposition="+x_values[i]+"&yposition="+y_values[i];
        }

        $.post($(this).attr('action'), dataToSubmit);   
    });

使用我当前的代码,当表单提交时,它直接进入控制器,然后我的 jquery 代码中的 $.post 调用调用同一个控制器,并在 $.post 调用的数据中传递 xpostion 和 yposition(即我想要的是)。

我怎样才能做到这一点,只有我的表单不会被提交两次——一次是在单击提交按钮后立即点击控制器,一次是从我的 $.post 调用中?

我想 :remote => true 使它不会直接调用控制器?

编辑

如果 :remote => true 发送 ajax 请求而我不必使用 jquery 的 $.post 方法手动发送 ajax 请求,我如何发送额外的变量以及使用 :remote => true 自动生成的 ajax 请求?

编辑

这是我的服务器日志中的一个片段

Started PUT "/applications/3" for 127.0.0.1 at 2013-01-18 00:06:19 -0600
Processing by ApplicationsController#update as */*
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3LOzuiL/PU6HypJ4OeN5H9yrX3Xyk0VT6XpcFYd1wY0=", "application"=>{"application_field_attributes"=>{"0"=>{"id"=>"8"}, "1"=>{"id"=>"9"}}}, "field_name"=>"aaa", "xposition"=>"0", "yposition"=>"0", "id"=>"3"}
  Account Load (0.3ms)  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = 'localhost' LIMIT 1
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Application Load (0.4ms)  SELECT `applications`.* FROM `applications` WHERE `applications`.`id` = 3 LIMIT 1


Started PUT "/applications/3" for 127.0.0.1 at 2013-01-18 00:06:19 -0600
Processing by ApplicationsController#update as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3LOzuiL/PU6HypJ4OeN5H9yrX3Xyk0VT6XpcFYd1wY0=", "application"=>{"application_field_attributes"=>{"0"=>{"id"=>"8"}, "1"=>{"id"=>"9"}}}, "commit"=>"Submit", "id"=>"3"}
  Account Load (0.3ms)  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`subdomain` = 'localhost' LIMIT 1
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Application Load (0.4ms)  SELECT `applications`.* FROM `applications` WHERE `applications`.`id` = 3 LIMIT 1
4

2 回答 2

1

您可以通过阻止其默认行为来阻止其正常提交。为此,向您的函数添加一个事件参数并调用.preventDefault()

$('.organizer').submit(function(e) {
        e.preventDefault();
        var dataToSubmit = $(this).serialize();
        var field_values = [];
        var x_values = [];
        var y_values = [];

        // i add values to the arrays above here

        for(i = 0; i < field_values.length; ++i) {
            dataToSubmit += "&field_name="+field_values[i]+"&xposition="+x_values[i]+"&yposition="+y_values[i];
        }

        $.post($(this).attr('action'), dataToSubmit);   
    });
于 2013-01-18T05:48:26.490 回答
1

您可以尝试停止除您之外的所有处理程序。试试这个:

$('.organizer').submit(function(e) {
  e.preventDefault(); // stops default behavior
  e.stopPropagation(); // prevents event bubbling
  // you code
  return false;
});
于 2013-01-18T06:19:07.487 回答