Rails 3.2, how to write a route for the put HTTP verb and use it in link_to?

I am trying to write two links for reject & approve actions, but I don’t know how to write the correct route,

my route.rb

put 'approve_class_room/:id(.:format)', :to => 'class_room_member_ships#approve'
put 'reject_class_room/:id(.:format)', :to => 'class_room_member_ships#reject'

but in rake routes I get:

PUT    /approve_class_room/:id(.:format) class_room_member_ships#approve
PUT    /reject_class_room/:id(.:format)  class_room_member_ships#reject

So what would be the correct link_to path?

my link

= link_to 'approve', approve_class_room_path

it does not work, I get:

undefined local variable or method `approve_class_room_path'

ps: I'm trying to use link_to using AJAX to claim on the same page, AM Am I on the right track? What will be the link_to path?

Any idea please?

+5
source share
2 answers

First, to clear the error, you need to use named routes :

put 'approve_class_room/:id(.:format)', :to => 'class_room_member_ships#approve',
                                        :as => :approve_class_room
put 'reject_class_room/:id(.:format)', :to => 'class_room_member_ships#reject',
                                       :as => :reject_class_room

Then, to execute the PUT request, you need to include the parameter :methodin your call link_to:

link_to 'approve', approve_class_room_path, :method => :put
link_to 'reject', reject_class_room_path, :method => :put

404, GET PUT, , :method => :put JavaScript. , jquery-rails .

+6

, , , link_to:

= link_to 'approve', approve_class_room_path, :method => :put

, , :

put 'approve_class_room/:id(.:format)', :to => 'class_room_member_ships#approve', :as => :approve_class_room
+1

All Articles