WozPoz
WozPoz

Reputation: 992

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

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' stuff 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

Upvotes: 4

Views: 1028

Answers (1)

pushmatrix
pushmatrix

Reputation: 746

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. :)

Upvotes: 2

Related Questions