[英]How do I create a singular GET path for a resource including helpers?
I have the following: 我有以下几点:
routes.rb
resources :splashes, only: [:index, :create, :destroy]
get '/splash', to: 'splashes#index'
>rake routes | grep splash
splashes GET /splashes(.:format) splashes#index
POST /splashes(.:format) splashes#create
splash DELETE /splashes/:id(.:format) splashes#destroy
GET /splash(.:format) splashes#index
However, when I try to use splash_url
, it generates http://localhost:3000/splashes/1
. 但是,当我尝试使用splash_url
,它将生成http://localhost:3000/splashes/1
splash_url
http://localhost:3000/splashes/1
。
I tried 我试过了
get '/splash', to: 'splashes#index', as: 'splash'
But it gave 但是它给了
rake routes | 耙路| grep splash grep飞溅
rake aborted! 耙子流产了!
ArgumentError: Invalid route name, already in use: 'splash' You may have defined two routes with the same name using the:as
option, or you may be overriding a route already defined by a resource with the same naming. ArgumentError:无效的路由名称,已经在使用:'splash'您可能已经使用:as
选项定义了两个具有相同名称的路由,或者您可能会覆盖具有相同名称的资源已定义的路由。 For the latter, you can restrict the routes created withresources
as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created 对于后者,您可以按照以下说明限制使用resources
创建的路由: http : //guides.rubyonrails.org/routing.html#restricting-the-routes-created
I tried to use the singular resource
, but it doesn't generate an #index
action, and you can't DELETE a specific splash row. 我尝试使用单数resource
,但它不会生成#index
操作,并且您不能删除特定的初始行。
Just place your singular routes get statement before the resources statement, 只需将单数路线的get语句放在resources语句之前,
get '/splash', to: 'splashes#index'
resources :splashes, only: [:index, :create, :destroy]
Now, the rake routs will give your the below result, 现在,耙子行将给您以下结果,
splash GET /splash(.:format) splashes#index
splashes GET /splashes(.:format) splashes#index
POST /splashes(.:format) splashes#create
DELETE /splashes/:id(.:format) splashes#destroy
By that above approach the delete and get routes get overrided based on the defintion sequence, because they both has the same named helpers spash_path . 通过上述方法, 删除和获取路由将根据定义顺序被覆盖,因为它们都具有相同的命名助手spash_path 。 So, I suggest you to add named helper to custom get /spash routes like below, 因此,建议您将命名辅助程序添加到自定义的get / spash路由中,如下所示,
resources :splashes, only: [:index, :create, :destroy]
get '/splash', to: 'splashes#index', as: 'splash_index'
So, now you will get a separate named routes splash_index for your custom get route. 因此,现在您将为您的自定义获取路线获得一个单独的命名路线splash_index 。 There's a another solution also, 还有另一种解决方案,
resources :splashes, only: [:index, :create, :destroy]
resources :splash, only: [:index], controller: :splashes
So, that you will get a different named helper for your /spash routes, 因此,您将为/ spash路由获得一个不同的命名帮助器,
splashes GET /splashes(.:format) splashes#index
POST /splashes(.:format) splashes#create
splash DELETE /splashes/:id(.:format) splashes#destroy
splash_index GET /splash(.:format) splashes#index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.