当您将控制器公开为资源时,会自动添加以下操作:
show
index
new
create
edit
update
destroy
这些操作可以分为两组:
成员操作的 URL 具有目标资源的 ID。例如:
users/1/edit
users/1
您可以将:member 操作视为类的实例方法。它始终适用于现有资源。
默认成员操作:show、edit、update、destroy
:collection 操作的 URL 不包含目标资源的 ID。例如:
users/login
users/register
您可以将:collection 操作视为类的静态方法。
默认收集操作:index、new、create
在您的情况下,您需要两个新的注册操作。这些操作属于 :collection 类型(因为您在提交这些操作时没有用户的 id)。您的路线可以如下:
map.resources :users, :collection => { :signup => :get, :register => :post }
动作的网址如下:
users/signup
users/register
如果您想删除 Rails 生成的标准操作,请使用 :except/:only 选项:
map.resources :foo, :only => :show
map.resources :foo, :except => [:destroy, :show]
编辑 1
我通常将confirmation 操作视为:member 操作。在这种情况下,params[id] 将包含确认码。
路线配置:
map.resources :users, :member => { :confirm => :get}
网址
/users/xab3454a/confirm
confirm_user_path(:id => @user.confirmation_code) # returns the URL above
控制器
class UsersController < ApplicationController
def confirm
# assuming you have an attribute called `confirmation_code` in `users` table
# and you have added a uniq index on the column!!
if User.find_by_confirmation_code(params[id])
# success
else
# error
end
end
end