4

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

1
  • Is it just? params[:urlpath] ... Anything I need to do to prevent SQL injections XSS etc? Commented Sep 26, 2010 at 21:17

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Any worries about XSS SQL Injection etc with this? As it just takes a params? Also, that seems to be encoding the URL, how do I decode it in the backend Rails app?
Is the urlpath entered by the user? Also, is it encoded before it gets sent through ajax, or is it encoded once it reaches the controller?
Well it's not entered by the user but it's taken from the browser URL which a user could modifiy. A user could also post an invalid variable to try to hack the system right? Just curious if params safe guards against that or if I need to add something extra?
I was wondering if the urlpath was entered by the user to figure out where the encoding was happening. I made a sample app that passes a url through the data attribute, and it was not encoded. As for XSS, I don't believe there is any risk. How will you be determining which sidebar to render? With a simple switch or if statement that looks at the urlpath?
Ya, with an IF statement, here it is.. Thoughts? "<% if params[:urlpath] && params[:urlpath].to_s.index('/projects') == 0 %>"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.