开发者

jQuery AJAX to Rails 3, In Rails how to get the Variable Passed via Data?

开发者 https://www.devze.com 2023-01-17 21:35 出处:网络
Rails 3 app.... I have the following jQuery which is working: $.ajax({ url: \'/navigations/sidenav\', data: \"urlpath=\" + urlpath,

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 can be paths like '/' or '/projects' or '/authors' s开发者_如何转开发tuff like that.

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?

Thanks


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");
    }
});

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

params[:urlpath]

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

0

精彩评论

暂无评论...
验证码 换一张
取 消