Rails 嵌套 URL 问题
我在 Rails 中遇到 RESTful URL 问题。
我有 site.com/services url,我想在该类别下有子页面,就是这样:site.com/services/arquitecture、site.com/services/plumbing 等。
我在该类别下提供的页面是“ static”.rhtml 文件,我希望它们位于同一控制器上。
有办法做到这一点吗?我尝试过嵌套资源,但发现很难完全理解。
谢谢
Im having issues with RESTful URLs in Rails.
I have site.com/services url, and I want to have subpages under that category, thats it: site.com/services/arquitecture, site.com/services/plumbing, etc.
The pages that im serving under that category are "static" .rhtml files and I would want them to be on the same controller.
Is there a way of doing this? I've tried nested resources but I find it hard to fully understand.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
资源不是为提供静态页面而构建的。使用常规的非 RESTful 路由,您可以准确定义哪些 URL 映射到哪个控制器和操作。
Resources weren't built for serving static pages. Use regular, non-RESTful routes where you can define exactly which URLs maps to which controller and action.
这是一种简单的方法。
假设您的routes.rb中有一个“服务”资源,则不需要嵌套资源 - 只需将 :members 哈希添加到您的路由定义中:
然后在服务控制器中为每个静态页面定义空操作。如果这些页面确实是静态的并且是静态的,则可以对它们使用页面缓存。在第一次调用每个操作后,Rails 将完全绕过控制器。
Here is one simple approaches for this.
Assuming you have a "services" resource in your routes.rb, you don't need nested resources - just add a :members hash to your route definition:
then define empty actions in your services controller for each static page. You can use page caching for those pages if they are truly static & Rails will bypass the controller altogether after the 1st call to each action.