更改 URL 而不更改资源名称
我正在为养兔者建立一个网站(让我们假设一下)。这个男人密切关注着他的兔子,并希望将它们全部分类。所以我为他构建了一个 RabbitCategoriesController,并将这一行添加到我的routes.rb 中。URL
resources :rabbit_categories
显示为 rabbit_categories
、rabbit_categoriew/new
等。
如果我想要 URL,该怎么办看起来像 rabits/categories
rabits/categories/new
吗?这不是嵌套资源,我只是想更改 URL 的外观。
当然,如果资源被称为“类别”,我可以做
namespace :rabbits do
resources :categories
end
有什么方法可以编写它,但告诉它使用 RabbitCategoriesController 而不是 Rabbits::CategoriesController ?
I'm building a website for a rabbit farmer (let's pretend). This man keeps a close eye on his rabbits, and wants them all categorized. So I built him a RabbitCategoriesController, and added this line to my routes.rb
resources :rabbit_categories
The URLs are showing up as rabbit_categories
, rabbit_categoriew/new
, etc.
What if I want the URLs to look like rabits/categories
rabits/categories/new
instead? This is not a nested resource, I just want to change the way the URLs look.
Of course, if the resources were called "categories", I could do
namespace :rabbits do
resources :categories
end
Is there any way I can write that, but tell it to use the RabbitCategoriesController instead of the Rabbits::CategoriesController?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过这个吗,应该可以。
请参阅Rails Routing from Outside In了解更多详细信息。
have you tried this, should work
See Rails Routing from Outside In for more details.