Ajax post提交表单ruby on rails

时间:2022-11-24 12:53:23

Where actually I can get my variable pass to my js.erb, let say

我可以把变量传递给js。erb,让说

I have 1 method create,in controller in my application.js, i have my Ajax post then I have my create.js.erb

我有一个方法创建,在我的应用程序控制器中。js,我有Ajax贴子,然后有create。js。erb

In my create form, I have 2 hidden_field_tag In my create.js.erb , this is here where the ajax success post and do the action, but how do I access the hidden_field data to carry out my logic to perform certain action when success?

在我的create表单中,我的create.js中有2个hidden_field_tag。erb,这里是ajax成功发布和执行操作的地方,但是我如何访问hidden_field数据来执行我的逻辑,在成功时执行某些操作?

This is due to, when it's js success, I got 2 different action to determine which action I should process. How actually js.erb works? Besides, when I alert the form data in application.js, it dont come out the alert although the js is working perfectly. Any idea?

这是因为,当js成功时,我有两个不同的动作来决定我应该处理哪个动作。实际上js。erb作品吗?此外,当我在应用程序中警告表单数据时。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;
  })
})

1 个解决方案

#1


3  

Wait, when you do this, lets say your :controller => "months" and :action => "march".

等一下,当您这样做时,让我们假设您的:controller =>“月”和:action =>“三月”。

I mean your $(this).action() returns "/months/march".

我是说你的$(这个).action() return "/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.

这将存储在url中,当您通过下面的post方法访问该url时,表单元素可以在params散列中的“月”控制器的“march”操作中获得。

Now, in the "result" js variable, the whole params hash will be available, which is actually you are alerting.

现在,在“result”js变量中,整个params散列都是可用的,这实际上是您正在警告的。

months_controller.rb

months_controller.rb

def march

  render :text => params

end

application.js

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;

          });

#1


3  

Wait, when you do this, lets say your :controller => "months" and :action => "march".

等一下,当您这样做时,让我们假设您的:controller =>“月”和:action =>“三月”。

I mean your $(this).action() returns "/months/march".

我是说你的$(这个).action() return "/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.

这将存储在url中,当您通过下面的post方法访问该url时,表单元素可以在params散列中的“月”控制器的“march”操作中获得。

Now, in the "result" js variable, the whole params hash will be available, which is actually you are alerting.

现在,在“result”js变量中,整个params散列都是可用的,这实际上是您正在警告的。

months_controller.rb

months_controller.rb

def march

  render :text => params

end

application.js

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;

          });