我有一个带有大约 60 多个动作的 Rails 控制器。我需要将其更改为仅允许对大约 20 个操作的 POST 请求以及对其余操作的任何请求方法。
有没有办法做到这一点,所以我不必手动指定所有路线都允许的路线?
这就是我目前所拥有的(并且有效):
post_regex = /first_route|second_route/
all_routes_regex = /third_route|fourth_route/
map.connect '/myroute/:id/:action', :controller => 'my_controller', :constraints => {:action => post_regex }, :conditions => { :method => :post }
map.connect '/myroute/:id/:action', :controller => 'my_controller', :constraints => {:action => all_routes_regex }
我尝试创建这样的东西,但它只会导致 RoutingError。
post_regex = /first_route|second_route/
class AllRoutesConstraint
def self.matches?(request)
(request.query_parameters[:action] !~ post_regex)
end
end
map.connect '/myroute/:id/:action', :controller => 'my_controller', :constraints => {:action => post_regex }, :conditions => { :method => :post }
map.connect '/myroute/:id/:action', :controller => 'my_controller', :constraints => {:action => AllRoutesConstraint }