ruby-on-railslink-to-remote

link_to_remote in rails, problem pass :id


i have problem use link_to_remote

link_to_remote document example say

link_to_remote "Delete this post", :update => "posts", :url => { :action => "destroy", :id => post.id }

this code make below html code

<a href="#" onclick="new Ajax.Updater('posts', '/blog/destroy/3', {asynchronous:true, evalScripts:true}); return false;">Delete this post</a>

but my app don't. my html is

<a href="#" onclick="new Ajax.Updater('posts', '/blog/6', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('2C4Yo8OIDN+dm9oieL37uRg++PuWa8LCz18gW5Cu+Vg=')}); return false;">Delete this post</a>

where is destroy in url?

i expected '/blog/destroy/6' but actually 'blog/6'

what's the problem?

my rails version is 2.3.5


append question

really my problem in update action

i want use below code

link_to_remote "toggle", :url => { :action => "update", :id => post.id }

but this code don't 'blog/update/6' it's also 'blog/6'...

if click link make this error

ActionController::UnknownAction (No action responded to 6. Actions: create, index, new, show, and update):

.... my route.rb is default

ActionController::Routing::Routes.draw do |map|
  map.resources :aaa

  map.resources :timetables
  map.resources :alarams
  map.resources :users

  map.login 'login', :controller => 'user_sessions', :action => 'new'  
  map.logout 'logout', :controller => 'user_sessions', :action => 'destroy'  
  map.resources :user_sessions  

  # The priority is based upon order of creation: first created -> highest priority.

  # Sample of regular route:
  #   map.connect 'products/:id', :controller => 'catalog', :action => 'view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
  # This route can be invoked with purchase_url(:id => product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   map.resources :products

  # Sample resource route with options:
  #   map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }

  # Sample resource route with sub-resources:
  #   map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller

  # Sample resource route with more complex sub-resources
  #   map.resources :products do |products|
  #     products.resources :comments
  #     products.resources :sales, :collection => { :recent => :get }
  #   end

  # Sample resource route within a namespace:
  #   map.namespace :admin do |admin|
  #     # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
  #     admin.resources :products
  #   end

  # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
  map.root :controller => "welcome"

  # See how all your routes lay out with "rake routes"

  # Install the default routes as the lowest priority.
  # Note: These default routes make all actions in every controller accessible via GET requests. You should
  # consider removing or commenting them out if you're using named routes and resources.
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end

Solution

  • You need to add :method => 'delete' to your link parameters. It seems that you have declared posts as a resource in your routing table.