简体   繁体   English

如何为包括助手的资源创建单个GET路径?

[英]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 with resources 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.

相关问题 如何确定资源的范围,以便对路径助手进行范围界定? - How can I scope a resource so that the path helpers are scoped? 如何设置到同一资源的单数和复数路由? - How do I setup singular and plural routes to the same resource? Rails:如何使用polymorphic_path动态为单个嵌套资源创建路径 - Rails: How to dynamically create path for singular nested resource using polymorphic_path 如何正确覆盖Rails中的_path帮助器? - How do I override _path helpers in rails properly? 如何在Rails 3中获取资源的绝对路径? - How do I get the absolute path of a resource in Rails 3? 如何使用GET创建路径? - How do I use a GET create path? 我在Rails应用程序中有一个名为Image的资源。 如何访问动态生成的路径助手? - I have a resource named Image in a Rails app. How can I access the dynamically generated path helpers? 如何在自定义“get”路由中使用路径助手 - how to use path helpers with a custom `get` route 如何使用单表继承(STI)在Ruby on Rails中为继承的资源生成适当的路由助手 - How do I generate appropriate route helpers for an inherited resource in Ruby on Rails using single table inheritance (STI) 如何在Rails 3.1中为作用域/嵌套资源创建named_route_path? - How do I create a named_route_path for a scoped/nested resource in rails 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM