阿贾克斯后提交导轨形式红宝石 [英] Ajax post submit form ruby on rails

查看:107
本文介绍了阿贾克斯后提交导轨形式红宝石的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里其实我可以让我的变量传递给我的js.erb,让说

我有1方法来创建,在控制器 在我的application.js,我有我的阿贾克斯后 然后,我有我的create.js.erb

在我创建的形式,我有2 hidden_​​field_tag 在我create.js.erb,这正是在这些地方AJAX成功后,做的动作,但我怎么访问hidden_​​field数据来进行我的逻辑来执行某些操作时成功?

这是因为,当它是JS的成功,我有2个不同的动作,以确定哪些行动,我应该处理。 如何真正js.erb作品?此外,当我提醒在application.js中的表单数据,它不出来的警报虽然JS的工作完美。你知道吗?

  $(文件)。就绪(函数(){
  $(#new_answer)。递交(函数(){
    $。员额(this.action,$(本).serialize(),NULL,脚本);
    警报(您所提交的+ $(本).serialize()); //它不力的工作,但它确实交
    返回false;
  })
})
 

解决方案
  

等待,当你做到这一点,可以说你:控制器=>月和   :行动=>三八

我的意思是你的$(本).action()返回/月/ 3月。

  

这将存储在URL,当你点击该URL通过以下方法后,表单元素在params哈希表的月控制器的长征行动可用。

现在,在结果JS变量,整个PARAMS哈希将可用,这其实就是你提醒。

months_controller.rb

 高清游行

  渲染:文本=> PARAMS

结束
 

的application.js

  $(#形式new_answer)。递交(函数(){

          VAR数据= $('#形式new_answer')序列化()。

          VAR URL = $(本).action();

           $。员额(URL,数据功能(结果){

            警报(结果);

            });

           返回false;

          });
 

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

I have 1 method create,in controller in my application.js, i have my Ajax post then I have my 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?

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?

$(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".

I mean your $(this).action() returns "/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.

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

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;

          });

这篇关于阿贾克斯后提交导轨形式红宝石的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆