5

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 is

= link_to 'approve', approve_class_room_path

it wont' work, I get:

undefined local variable or method `approve_class_room_path'

p.s: I am trying to have the link_to works using AJAX, to do the approve in the same page, AM I on the right way ? what would be the link_to path ?

Any idea please ?

simo
  • 23,342
  • 38
  • 121
  • 218

2 Answers2

8

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 perform a PUT request, you need to include the :method option in your link_to call:

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

If you are getting a 404 that results in a GET request instead of a PUT request, it's because the :method => :put option relied on JavaScript. You'll need to make sure jquery-rails is properly integrated in your app.

Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
  • thanks Matt, can you please explain the (.:format) part in the named routes ? – simo Jun 01 '12 at 02:38
  • Since `(.:format)` is in parentheses, it's an optional part of the URL string for the route. The "." is static, while the ":format" is the actual option. Rails defaults to format being "html", but this allows you to access `approve_class_room/5.xml`, `approve_class_room/5.json`, etc. assuming you have configured your Rails app to handle those types of formats. Check out [respond_to](http://apidock.com/rails/ActionController/MimeResponds/respond_to) and [respond_with](http://apidock.com/rails/ActionController/MimeResponds/respond_with) if you're interested in handling multiple formats. – Matt Huggins Jun 01 '12 at 13:39
  • If you're not worried about formats, you can just leave your routes' URL strings as 'approve_class_room/:id' and 'reject_class_room/:id' without the '(.:format)' portion. – Matt Huggins Jun 01 '12 at 14:20
  • yes, I've removed the format, I just added :constraints => { :id => /\d+/ } to accept only numbers ... – simo Jun 03 '12 at 02:01
  • Not sure, but you may need to change `/\d+/` to `/\A\d+\z/` to match the start and end of the string. Don't quote me on that. Otherwise the string "hithere123foo" would pass because it contains numbers, even though the whole string is not numbers. – Matt Huggins Jun 03 '12 at 23:52
  • oh, I think I need to read more on regular expressions, thanks for the notice – simo Jun 04 '12 at 09:32
  • I've searched for switch \A and switch \z for ruby regular expressions, but couldn't find explanation, can you please explain them ? – simo Jun 04 '12 at 09:38
  • Check out [this answer](http://stackoverflow.com/a/5979643/107277) and its accompanying link to [Ruby docs](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UL). – Matt Huggins Jun 04 '12 at 13:51
1

This may not clear that error, but since the request is a put, you need to specify that in the link_to:

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

For the route, I think adding this would make the error go away:

put 'approve_class_room/:id(.:format)', :to => 'class_room_member_ships#approve', :as => :approve_class_room
Bill Turner
  • 3,695
  • 1
  • 20
  • 26
  • 1
    thanks, Bill, although your answer is correct, I didn't see the ":as => :approve_class_room" part in the first place, only when I scrolled the scroll bar, I wish I can mark two answers as correct, so here is an up vote :-) – simo Jun 01 '12 at 02:41