Rails:定制的嵌套控制器动作 [英] Rails: Custom nested controller actions
问题描述
我想设置一个自定义的嵌套控制器动作,但无法弄清楚路由。
I want to setup a custom nested controller actions but I can't figure out the routing.
我一直收到以下错误消息
I keep getting the following error
No route matches [GET] "/assets"
routes.rb
routes.rb
resources :companies do
resources :requests do
match :accept
end
end
index.html.rb
index.html.rb
<% @requests.each do |request| %>
<ul class="users">
<li>
<%= full_name(request.profile) %>
<%= request.status %>
<%= link_to "Accept",
:controller => "requests", :action => "accept",
:id => request.id %>
</li>
</ul>
<% end %>
推荐答案
有两个问题:路由到接受
操作并构建指向嵌套资源的URL。
There are a couple of problems: routing to the accept
action and building a URL to a nested resource.
您可以使用以下语法将自定义操作添加到RESTful资源中:
You can add custom actions to your RESTful resources using this syntax:
resources :requests do
get 'accept', :on => :member
end
这将为您提供一条看起来像这样的路线:
This will give you a route that looks like this:
requests/:id/accept
您可以使用以下方法在视图中生成路径:
And you can generate paths in your views using:
accept_request_path(a_request)
:on => :member
部分表示您将路由到每个单独的请求的新操作,而不是所有请求的集合。如果您使用:on => :collection
路线将是 requests / accept
The :on => :member
part indicates that you're routing to a new action on each individual request, rather than the collection of all requests. If you used :on => :collection
the route would be requests/accept
当您嵌套资源时:
resources :companies do
resources :requests do
get 'accept', :on => :member
end
end
您将获得如下所示的路线,请注意因为请求嵌套在公司内部,所以路由同时包含 company_id
和 id
:
You get routes that look like this, note that because the requests is nested inside companies the route includes both a company_id
and an id
:
companies/:company_id/requests/:id/accept
像这样的助手:
accept_company_request_path(a_company, a_request)
您可以像现在正在尝试的那样长时间进行此操作,例如:
You could do this long-hand, as you're currently trying to do, with something like:
<%= link_to "Accept",
:controller => "requests", :action => "accept",
:id => request.id, :company_id => request.company.id %>
但是使用帮助程序更容易:
But it's easier to use the helpers:
<%= link_to "Accept", accept_company_request_path(request.company, request) %>
适当的动词
接受声音很多例如以某种方式更新数据库的内容,如果是这种情况,则应考虑使用 PUT
请求而不是 GET
请求。
HTTP / 1.1规范说,已经建立了约定,即GET和HEAD方法不应具有其他动作的意义。比检索( RFC2616,第9 部分)具有现实世界中的含义是,允许非人类Web客户端(搜索引擎索引器,浏览器扩展等)跟随链接(发出 GET
请求),但不允许提交
The HTTP/1.1 spec says that the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval (RFC2616, section 9) which has the real-world implication that non-human web clients — search engine indexers, browser extensions, etc. — are allowed to follow links (which make GET
requests) but not allowed to submit forms that make other types of requests.
如果您确实转为使用 PUT
请求,则 button_to
助手会派上用场。与 link_to
一样,您可以将控制器,操作,方法和路由所需的所有参数传递给 button_to
:
If you do switch to using a PUT
request then the button_to
helper will come in handy. As with link_to
you can pass the controller, action, method, and all the parameters required by the route to button_to
:
<%= button_to 'Accept',
{:controller => :requests, :action => :accept,
:company_id => request.company, :id => request},
:method => :put %>
或者您可以使用帮助程序生成更简单的路径:
Or you can use the helpers to generate the path which is much easier:
<%= button_to 'Accept',
accept_company_request_path(request.company, request),
:method => :put %>
更多文档
- 添加更多RESTful行为
- 嵌套的资源
- Adding more RESTful actions
- Nested resources
More documentation
这篇关于Rails:定制的嵌套控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!