我正在尝试通过ajax提交表单,并包含一些我通过jquery设置的变量。
/views/applications/_form.html.erb
<%= form_for(@application, :html => {:class => "organizer"}, :remote => true) do |f| %>
// fields in here
<% end %>
/controllers/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 =&gt; true会使表单不会直接调用控制器吗?
修改
如果:remote =&gt; true发送ajax请求而不必使用jquery的$ .post方法手动发送ajax请求,我如何发送额外的变量以及使用以下命令自动获取的ajax请求:remote =&gt;真?
修改
这是我服务器日志中的一个片段
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
答案 0 :(得分:1)
您可以通过阻止其默认行为来阻止for正常提交。为此,请向您的函数添加一个事件参数,然后调用.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);
});
答案 1 :(得分:1)
您可以尝试停止除您之外的所有处理程序。试试这个:
$('.organizer').submit(function(e) {
e.preventDefault(); // stops default behavior
e.stopPropagation(); // prevents event bubbling
// you code
return false;
});