Ruby-on-rails 如何将自定义 RESTful 路由添加到 Rails 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2634964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a custom RESTful route to a Rails app?
提问by ma?ek
I'm reading these two pages
我正在阅读这两页
The Rails Guides page shows
Rails 指南页面显示
map.resources :photos, :new => { :upload => :post }
And its corresponding URL
以及它对应的网址
/photos/upload
This looks wonderful.
这看起来很棒。
My routes.rbshows this
我的routes.rb显示这个
map.resources :users, :new => { :signup => :get, :register => :post }
When I do: [~/my_app]$ rake routes
当我做: [~/my_app]$ rake routes
I see the two new routes added
我看到添加了两条新路线
signup_new_user GET /users/new/signup(.:format)
register_new_user POST /users/new/register(.:format)
Notethe inclusion of /new! I don'twant that. I just want /users/signupand /users/register(as described in the Rails Routing Guide).
注意包含/new! 我不想要那个。我只是想要/users/signup和/users/register(如 Rails 路由指南中所述)。
Any help?
有什么帮助吗?
回答by Harish Shetty
When you expose a controller as a resource, following actions are automatically added:
当您将控制器公开为资源时,会自动添加以下操作:
show
index
new
create
edit
update
destroy
These actions can be categorized in to two groups:
这些操作可以分为两组:
:memberactions
:member行动
The URL for the member action has the id of the target resource. E.g:
成员操作的 URL 具有目标资源的 id。例如:
users/1/edit
users/1
You can think of :memberaction as an instance method on a class. It always applies on an existing resource.
您可以将:member操作视为类上的实例方法。它始终适用于现有资源。
Default member actions: show, edit, update, destroy
默认成员操作:show, edit, update,destroy
:collectionactions
:collection行动
The URL for the :collectionaction does not contain the id of the target resource. E.g:
操作的 URL:collection不包含目标资源的 id。例如:
users/login
users/register
You can think of :collectionaction as a static method on a class.
您可以将:collection操作视为类上的静态方法。
Default collection actions: index, new, create
默认收集操作:index, new,create
In your case you need two new actions for registration. These actions belong to :collection type( as you do not have the id of the user while submitting these actions). Your route can be as follows:
在您的情况下,您需要两个新的注册操作。这些操作属于 :collection 类型(因为您在提交这些操作时没有用户的 id)。您的路线可以如下:
map.resources :users, :collection => { :signup => :get, :register => :post }
The URL for the actions are as follows:
操作的 URL 如下:
users/signup
users/register
If you want to remove a standard action generated by Rails use :except/:only options:
如果要删除由 Rails 生成的标准操作,请使用 :except/:only 选项:
map.resources :foo, :only => :show
map.resources :foo, :except => [:destroy, :show]
Edit 1
编辑 1
I usually treat the confirmationaction as a :memberaction. In this case params[id]will contain the confirmation code.
我通常把这个confirmation动作当作一个:member动作。在这种情况下params[id]将包含确认代码。
Route configuration:
路由配置:
map.resources :users, :member => { :confirm => :get}
URL
网址
/users/xab3454a/confirm
confirm_user_path(:id => @user.confirmation_code) # returns the URL above
Controller
控制器
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
回答by prashantsunkari
This can be taken as just another syntax -- something good to know may be.
这可以看作是另一种语法——一些值得了解的东西。
Syntax 1:
语法 1:
resources :users do
member do
get 'signup'
post 'register'
end
end
Rake Route Output will include
耙路线输出将包括
signup_users GET /users/signup(.:format) {:action=>"signup", :controller=>"users"}
register_users POST /users/register(.:format) {:action=>"register", :controller=>"use
rs"}
rs"}
Syntax 2:If you have only one collection route
语法 2:如果您只有一个收集路线
resources :users do
get 'signup', :on => :collection
end
回答by mikezter
If i'm understanding your question right, you just want to rename the urls of the newand createactions.
如果我正确理解您的问题,您只想重命名new和create操作的网址。
This would be done like so:
这将像这样完成:
map.resources :users, :path_names => {:new => 'signup', :create => 'register'}
If you really would like to addnew routes with corresponding controller actions, then Damiens answer is the way to go.
如果您真的想添加具有相应控制器操作的新路由,那么 Damiens 的答案就是要走的路。
回答by Damien MATHIEU
The newoption allows you to create new routes for creating new objects. That's why they're prefixed with that term.
该new选项允许您创建用于创建新对象的新路径。这就是为什么它们以该术语为前缀的原因。
What you're looking for is the :collectionoption.
您正在寻找的是:collection选项。
map.resources :users, :collection => { :signup => :get, :register => :post }
Which will create the /users/signupand /users/registerurls.
这将创建/users/signup和/users/registerurl。

