如何在 Rails 中自定义 RESTful 路由(基础)

发布于 2024-08-25 16:23:26 字数 1330 浏览 11 评论 0原文

我已通读 Rails 文档 路由Restful 资源,以及 UrlHelper,并且仍然不了解创建复杂/嵌套路由的最佳实践。我现在正在处理的示例是针对事件的,它有_many rsvps。因此,用户查看事件列表,然后单击注册,然后完成注册过程等。我希望网址如下所示:

/events
/events/123 # possible without title, like SO
/events/123/my-event-title # canonical version
/events/my-category/123/my-event-title # also possible like this
/events/123/my-event-title/registration/new
... and all the restful nested resouces.

问题是,如何使用最少的代码来完成此操作?

这就是我目前所拥有的:

map.resources :events do |event|
  event.resources :rsvps, :as => "registration"
end

这让我想到:

/events/123/registration

完成其他两条路线的最佳方法是什么?

/events/123/my-event-title # canonical version
/events/my-category/123/my-event-title # also possible like this

其中 my-category 只是事件可能的 10 种可能类型的数组。

我已修改 Event#to_param 以返回 "#{self.id.to_s}-#{self.title.parameterize}",但我更愿意/id/title 具有完整的规范性

I have read through the Rails docs for Routing, Restful Resources, and the UrlHelper, and still don't understand best practices for creating complex/nested routes. The example I'm working on now is for events, which has_many rsvps. So a user's looking through a list of events, and clicks register, and goes through a registration process, etc. I want the urls to look like this:

/events
/events/123 # possible without title, like SO
/events/123/my-event-title # canonical version
/events/my-category/123/my-event-title # also possible like this
/events/123/my-event-title/registration/new
... and all the restful nested resouces.

Question is, how do I accomplish this with the minimal amount of code?

Here's what I currently have:

map.resources :events do |event|
  event.resources :rsvps, :as => "registration"
end

That gets me this:

/events/123/registration

What's the best way to accomplish the other 2 routes?

/events/123/my-event-title # canonical version
/events/my-category/123/my-event-title # also possible like this

Where my-category is just an array of 10 possible types the event can be.

I've modified Event#to_param to return "#{self.id.to_s}-#{self.title.parameterize}", but I'd prefer to have /id/title with the whole canonical-ness

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笔绘流年 2024-09-01 16:23:26

由于您描述的 SEO 友好 URL 不符合 Rails 生成 RESTful 路由的方式,因此您需要使用常规路由来设置它们。例如:

map.show_event 'events/:id/:event_title', :controller => 'events', :action => 'show'

不要觉得您必须对应用程序中的所有内容使用自动生成的 RESTful 路由。它并不总是适合您想要做的事情。甚至 Rails 路由指南也这么说:

虽然 RESTful 路由已成为
Rails 标准,还有很多
更简单常规的地方
路由工作正常。你甚至可以混合
单一风格中的两种风格
应用。一般来说,您应该
如果可能的话,更喜欢 RESTful 路由,
因为它会让你的一部分
应用程序更容易编写。但
没有必要尝试用鞋拔子
你的申请的最后一部分
如果不是,则进入 RESTful 框架
很合适。

Because the SEO-friendly URLs you describe do not conform to the way Rails generates RESTful routes, you will need to use regular routes to set these up. For example:

map.show_event 'events/:id/:event_title', :controller => 'events', :action => 'show'

Don't feel that you have to use the automatically generated RESTful routes for absolutely everything in your application. It's not always a good fit for what you're trying to do. Even the Rails routing guide says so:

While RESTful routing has become the
Rails standard, there are still plenty
of places where the simpler regular
routing works fine. You can even mix
the two styles within a single
application. In general, you should
prefer RESTful routing when possible,
because it will make parts of your
application easier to write. But
there’s no need to try to shoehorn
every last piece of your application
into a RESTful framework if that’s not
a good fit.

明天过后 2024-09-01 16:23:26

您是否看过 Rails 路由指南?它提供了大量信息来帮助您了解路由器,并包含有关嵌套资源。

Have you looked at the Rails routing guide? It's got lots of info to get you understanding the router and includes a section on nested resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文