Search code examples
jqueryruby-on-railsujs

Rails 3.2 UJS -> Can it grab the ID for me for a dynamic URL?


I need to construct the POST url for a resouce and the val() of the item that I am adding remote-true to will be the ID in the URL resource. IS there a rails shortcut for doing this or do I just have to do

  data: {'id': $(this).val()}

in the jQuery ajax call?


Solution

  • Not sure I understood your problem, but I guess you want to achieve this

    data: {'id': '<%= params[:id] %>'}
    

    You can access the id contained in the url with params[:id] and embed this in your javascript snippet (unless it is in a .js file).

    Is this addressing your problem? If not can you please clarify / rephrase?