Search code examples
ruby-on-railslink-to-remote

rails how to call another controller in link_to_remote


how to call a different controller's action using link_to_remote

:url => {:controller => "Posts", :action => "update"} doesn't work


Solution

  • the method:

    link_to_remote(name, options = {}, html_options = nil)
    

    passing in a hash like:

    link_to_remote "hug kittens", { :url => { :controller => 'kittens', :action => 'show' } }
    

    as the second argument (options) works. verified.

    the result:

    <a onclick="new Ajax.Request('/kittens/hug', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('/BdZwHdC/QqtBJsdCU+cCHxabHj/QHUT6i8ggbr5CtY=')}); return false;" href="#">hug kittens</a>
    

    The problem with your implementation might be, that there is no "real" update-url (except you created one by hand). Please have a look at the url of your edit-form. It's actually a post-request to "posts/:post_id".