实际上我可以在哪里将变量传递给我的 js.erb,比如说
我有 1 个方法创建,在 Controller 中 在我的 application.js 中,我有我的 Ajax 帖子 然后我就有了 create.js.erb
在我的创建表单中,我有 2 个hidden_field_tag 在我的 create.js.erb 中,这是 ajax 成功发布并执行操作的地方,但是如何访问 hide_field 数据来执行我的逻辑,以在成功时执行某些操作?
这是因为,当 js 成功时,我有 2 个不同的操作来确定我应该处理哪个操作。 js.erb 实际上是如何工作的?此外,当我对 application.js 中的表单数据发出警报时,尽管 js 工作正常,但它并没有发出警报。有什么想法吗?
$(document).ready(function() {
$("#new_answer").submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
alert("you are submitting" + $(this).serialize()); //it dint work but it did post
return false;
})
})
最佳答案
Wait, when you do this, lets say your :controller => "months" and :action => "march".
我的意思是你的 $(this).action() 返回“/months/march”。
This will stored in url and when you hit that url by the below post method, the form elements are available in the "march" action of the "months" controller in the params hash.
现在,在“result”js 变量中,整个 params 哈希将可用,这实际上是您正在警告的。
months_controller.rb
def march
render :text => params
end
application.js
$("form#new_answer").submit(function() {
var data = $('form#new_answer').serialize();
var url = $(this).action();
$.post(url ,data, function(result){
alert(result);
});
return false;
});
关于ruby-on-rails - Ajax 提交表单 ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15405328/