[英]RESTful Rails 3 routing
I'm new to RESTful design and confused: if I make PUT, GET or POST to a same resource, say, /weblogs/myweblog, how should I write in the route.rb, and related controller? 我是RESTful设计的新手,感到困惑:如果我将PUT,GET或POST放在同一个资源(例如/ weblogs / myweblog)中,应该如何在route.rb和相关控制器中编写? Does the following works?
以下工作有效吗? In route.rb
在route.rb中
match 'weblogs/myweblog/new' => 'weblogs#create_new_blog'
match 'weblogs/myweblog/edit/:id' => 'weblogs#edit_blog'
.
.
In weblogs_controller.rb 在weblogs_controller.rb中
def create_new_blog
...
end
def edit_blog
params[:id]..
....
end
and confused if I want to do GET/PUT/POST on the same resource, if their URL is same but only HTTP request is different, how to write different operations in the controller? 如果我想在同一资源上执行GET / PUT / POST,并且感到困惑,如果它们的URL相同但只有HTTP请求不同,那么如何在控制器中编写不同的操作?
In general it's best to define your routes in terms of resources, so if you have a resource named webblog
, your routes can be defined using just: 通常,最好根据资源来定义路由,因此,如果您有一个名为
webblog
的资源,则可以使用以下命令定义路由:
resources :weblogs
If you check the routes generated by this (with rake routes
), you will see that it defines a standard set of mappings from GET
, PUT
, POST
and DELETE
actions on urls to controller actions: 如果您检查由此生成的路由(使用
rake routes
),您会看到它定义了从url的GET
, PUT
, POST
和DELETE
操作到控制器操作的标准映射集:
webblogs GET /weblogs(.:format) weblogs#index
POST /weblogs(.:format) weblogs#create
new_webblog GET /weblogs/new(.:format) weblogs#new
webblog GET /weblogs/:id(.:format) weblogs#show
PUT /weblogs/:id(.:format) weblogs#update
DELETE /weblogs/:id(.:format) weblogs#destroy
These routes will map to standard controller actions index
, create
, new
, show
, etc. 这些路由将映射到标准控制器操作
index
, create
, new
, show
等。
If for whatever reason you want to define routes without using resources
, you can define them separately: 如果出于任何原因要定义路由而不使用
resources
,则可以分别定义它们:
get '/weblogs' => 'weblogs#index'
get '/weblogs/new' => 'weblogs#new'
get '/weblogs/:id/edit' => 'weblogs#edit'
put '/weblogs/:id' => 'weblogs#update'
...
By defining routes with get
, put
etc. you can map a single URL to multiple controller actions, eg like this: 通过使用
get
, put
等定义路由,您可以将单个URL映射到多个控制器操作,例如:
get '/weblogs/myweblog' => 'weblogs#show_myweblog'
put '/weblogs/myweblog' => 'weblogs#update_myweblog'
post '/weblogs/myweblog' => 'weblogs#create_myweblog'
destroy '/weblogs/myweblog' => 'weblogs#destroymy_weblog'
This will map the URL /weblogs/myweblog to the method show_myweblog
for a GET request, update_myweblog
for a PUT request, create_myweblog
for a POST request, and destroy_myweblog
for a DELETE request. 这将映射URL /维博/ myweblog的方法
show_myweblog
为GET请求, update_myweblog
对于PUT请求, create_myweblog
对POST请求,并destroy_myweblog
为一个DELETE请求。
Alternatively, using the standard resources
, you can pick and choose which routes you want from the full set with the only
option: 或者,使用标准
resources
,您可以使用only
选项从全套中选择所需的路线:
resources :weblogs, only: [:show, :edit]
See the documentation for more details. 有关更多详细信息,请参见文档 。 I hope this answers your question, if not please provide more details on what you want to do in the comments.
我希望这能回答您的问题,如果不能,请在评论中提供您想要做的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.