Rails 3 带有斜线和嵌套资源的路线

发布于 2025-01-05 21:28:59 字数 1068 浏览 2 评论 0原文

我在添加斜线段和嵌套路线时遇到问题。

如果我有这些路线:

resources :courses do
  resources :registrations
end

我有这些 URL:

/courses/7
/courses/7/registrations

如果我在 Course.rb 中更改 to_param,我可以在路线中发生一些 slugs:

def to_param
  "#{id}-#{slug}"
end

这然后给我:

/courses/7-title-of-course
/courses/7-title-of-course/registrations

到目前为止一切都很好。

我遇到的问题是在查看此 http://www.miguelsanmiguel.com/2011/03/17/slug-that-slash

如何让它与嵌套资源一起使用:

Course.rb:

def to_param
  "#{id}/#{slug}"
end

Routes.rb

resources :courses, :constraints => { :id => /[0-9]+\/.+/ } do
  resources :registrations
end

URL:

/courses/7/title-of-course
/courses/7/title-of-course/registrations

如果我设置的课程路线很好,但注册路线已损坏。

这里有什么建议吗?

I'm having trouble adding a slashed slug and nested routes.

If I have these routes:

resources :courses do
  resources :registrations
end

I have these URLs:

/courses/7
/courses/7/registrations

If I change to_param in Course.rb, I can get some slugs happening in the routes:

def to_param
  "#{id}-#{slug}"
end

This then gives me:

/courses/7-title-of-course
/courses/7-title-of-course/registrations

All good so far.

The problem I'm having is after looking at this http://www.miguelsanmiguel.com/2011/03/17/slug-that-slash:

How do I get this to work with nested resources:

Course.rb:

def to_param
  "#{id}/#{slug}"
end

Routes.rb

resources :courses, :constraints => { :id => /[0-9]+\/.+/ } do
  resources :registrations
end

URL:

/courses/7/title-of-course
/courses/7/title-of-course/registrations

If I set things up like that the Course route is fine but the registration routes are broken.

Any tips here?

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

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

发布评论

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

评论(1

凉栀 2025-01-12 21:28:59

尝试添加约束:

resources :courses, :constraints => { :id => /.*/ } do
  resources :registrations
end

Try adding constraints:

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