1

I have this JQuery code:

    $("div.notification").live("click", function(){
        window.location=$(this).find("a").attr("href");
        var myId = $(this).attr("id");
        $.post("/videos/selected_notification.js", myId);
        return false;
    });

I want to be able to access myId from inside my controller that the post request hits. How do I do this?

1 Answer 1

2

Give your myId value a name and you can pull it out of params just like any other POST parameter:

$.post("/videos/selected_notification.js", { id: myId });

And then, in the controller:

id = params['id'] # Or params[:id] since params is a HashWithIndifferentAccess
Sign up to request clarification or add additional context in comments.

4 Comments

ahh nice... what if I'm not actually POSTing anything. Would that code be OK?
also would it not be params[:id]?
@Justin: Why would you by using $.post if you're not posting anything? You have to post something to get your data to the server (or use GET if that's more appropriate). Accessing params with the :id symbol or 'id' string should work.
@Justin: params is actually a HashWithIndifferentAccess, not a plain Hash. The keys are actually strings but HashWithIndifferentAccess converts symbols to strings for you.

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.