在Rails 3中限制资源路由并添加其他非RESTful路由 [英] Restricting resource routes and adding additional non-RESTful routes in Rails 3
问题描述
我在这里或其他地方都找不到能够限制资源路由并在Rails 3中添加其他非RESTful路由的东西。这可能非常简单,但是我遇到的每个示例或解释都只涉及一种情况。
I wasn't able to find anything here or elsewhere that covered both restricting a resource's routes and adding additional non-RESTful routes in Rails 3. It's probably very simple but every example or explanation I've come across addresses just one case not both at the same time.
以下是我在Rails 2中所做的一个示例:
Here's an example of what I've been doing in Rails 2:
map.resources:sessions,:only => [:new,:create,:destroy],:member => {:recovery => :get}
很简单,我们只需要7条RESTful路由中的3条,因为其他路由对该资源没有任何意义,但是我们还想添加另一条用于帐户恢复的路由。
Pretty straightforward, we only want 3 of the 7 RESTful routes because the others don't make any sense for this resource, but we also want to add another route which is used in account recovery.
现在,根据我的收集,做以下任何一件事情也非常简单:
Now from what I gather doing either one of these things is very straightforward as well:
resources:sessions,:only => [:new,:create,:destroy]
就像在Rails 2中一样。并且:
Just like in Rails 2. And:
resources :sessions do
member do
get :recovery
end
end
那么,如何结合这两个?我仍然可以使用旧的Rails 2方法吗?在Rails 3中有一种首选的方法吗?
So, how do I combine these two? Can I still use the old Rails 2 way of doing this? Is there a preferred way of doing this in Rails 3?
推荐答案
您可以将参数和一个块传递给资源
:
You can pass arguments and a block to resources
:
resources :sessions, :only => [:new, :create, :destroy] do
get :recovery, :on => :member
end
并使用 rake route
。
这篇关于在Rails 3中限制资源路由并添加其他非RESTful路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!