Rails 3 带有斜线和嵌套资源的路线
我在添加斜线段和嵌套路线时遇到问题。
如果我有这些路线:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加约束:
Try adding constraints: