jQuery AJAX 到 Rails 3,在 Rails 中如何获取通过数据传递的变量? [英] jQuery AJAX to Rails 3, In Rails how to get the Variable Passed via Data?

查看:35
本文介绍了jQuery AJAX 到 Rails 3,在 Rails 中如何获取通过数据传递的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3 应用程序....我有以下正在运行的 jQuery:

Rails 3 app.... I have the following jQuery which is working:

$.ajax({
    url: '/navigations/sidenav',
    data: "urlpath=" + urlpath,
    success: function(e){
        $("#sideNav-container").slideDown("slow");
    }
});

urlpath 可以是诸如/"或/projects"或/authors"之类的路径.

urlpath can be paths like '/' or '/projects' or '/authors' stuff like that.

我的问题是如何在控制器中获取 urlpath 变量,以便我可以在我的视图中使用它来确定返回给用户的 sidenav?

My question is how do I grab that urlpath variable in the controller so I can use it in my view to determine what sidenav to return to the user?

谢谢

推荐答案

将 urlpath 以散列形式传入data"键.像这样:

Pass in the urlpath in a hash for the "data" key. Like so:

$.ajax({
    url: '/navigations/sidenav',
    data:{"urlpath":urlpath},
    success: function(e){
        $("#sideNav-container").slideDown("slow");
    }
});

这将在 params 对象中传递 urlpath,您可以从控制器访问该对象.所以你可以简单地做

This will then pass urlpath in the params object, that you can access from your controller. So you can simply do

params[:urlpath]

你可以得到你传入的urlpath.:)

and you can get the urlpath you passed in. :)

这篇关于jQuery AJAX 到 Rails 3,在 Rails 中如何获取通过数据传递的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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