首先,coreyward在浅层次上很好地提出并回答了这个问题,但我理解得不够深入。
如果我扩展科里沃德的例子,我可以想象一个致力于强调策展的照片网站。照片应该显示在相册中,一张照片可以出现在多个相册中。此外,照片还有很多标签。
所以 User has_many :albums、:photos 和 :tags、Album has_many :photos 和 :tags (通过照片)、Photo has_many :tags 等。
所以查看照片的主要方式将通过 /users/123 /相册/456/照片/789。照片将有一个唯一的 ID,因此我可以简单地访问 /photos/789,但我认为该 URL 在用户体验中是一个有价值的寻路工具,并且希望清楚地表明这是属于特定相册中的照片特定用户。和 coreyward 一样,我也想在一些地方使用虚荣 URL(url slugs)。
即使我以最简单的方式路由#show 和#index 操作(例如/photos/789 和/albums/456/photos),#new 和#create 也会有一些问题。如果我打开 /users/123/albums/456/photos/new,用户可以上传一张照片,这将获得 user_id 123 并且该应用程序将足够智能,可以创建一个连接表(album_photos 或您拥有的东西)条目,而无需要求用户在单独的步骤中进行此关联。
仍然存在这样的情况:某人根据权限想要查看 /users/123/photos 或只是 /photos、/albums、/tags。
我想避免像 coreyward 建议的控制器中的条件逻辑,但有 app/controllers/photos_controller.rb、app/controllers/users/photos_controller.rb 和 app/controllers/users/albums/photos_controller.rb 似乎很讨厌。
我应该如何处理这样的事情(在 Rails 3 中)?
谢谢!
First of all, coreyward asks and answers this question well on a shallow level, but I don't understand well enough.
If I expand on coreyward's example, I can imagine a photo site that's intent on emphasizing curation. Photos are meant to be displayed in albums, and one photo can appear in many albums. In addition, photos have many tags.
So User has_many :albums, :photos, and :tags, Album has_many :photos and :tags (by way of photos), Photo has_many :tags, etc.
So the main way of looking at a photo will be through /users/123/albums/456/photos/789. Photos will have a unique ID, so I could do simply access /photos/789, but I think the URL is a valuable wayfinding tool in the user experience and would prefer it to be clear that this is a photo in a particular album belonging to a particular user. Like coreyward, I would also like to use vanity URLs (url slugs) in a few places.
Even if I routed the #show and #index actions in the simplest ways possible (e.g. /photos/789 and /albums/456/photos), #new and #create would be a little more problematic. If I opened /users/123/albums/456/photos/new, the user could upload a photo, which would get the user_id 123 AND the app would be smart enough to create a join table (album_photos or what have you) entry without asking the user to make this association in a separate step.
There would still be scenarios where someone, depending on permissions, would want to see /users/123/photos or just /photos, /albums, /tags.
I'd like to avoid conditional logic n the controllers like coreyward suggested, but having app/controllers/photos_controller.rb, app/controllers/users/photos_controller.rb, and app/controllers/users/albums/photos_controller.rb seems nasty.
How should I deal with something like this (in Rails 3)?
Thanks!
发布评论
评论(1)
你完全搞错了伙计。在任何情况下,您都不必拥有
users/photos_controller.rb
。您可以只拥有users_controller.rb
和photos_controller.rb
,然后随心所欲地使用路由:查看 此处 查看一些示例,但请注意其中的内容:“资源嵌套深度不应超过 1 层”。事情很快就会变得非常混乱,尤其是当你必须开始写一些垃圾内容时,比如:
恶心!实际上,嵌套路由应该谨慎使用,并且不惜一切代价避免。您所描述的内容听起来根本不像嵌套路由的良好候选者(事实上,我可以加载没有相册的图片,这是一个危险信号)。除了通过路由之外,还有其他方法可以保护对资源的访问
You got it all wrong man. You don't have to have a
users/photos_controller.rb
under any circumstance. You can just have ausers_controller.rb
andphotos_controller.rb
and then get as fancy as you want with the routing:look here for some examples BUT pay attention where it says: "Resources should never be nested more than 1 level deep". Thing are going to get really messy really fast on you especially when you have to start writing junk like:
YUCK! Really, nested routing is something that should be used sparingly and avoided at all costs. What you are describing does not sound like a good candidate for nested routing at all (the fact that I can load up a picture without its album is a red flag). There are other ways to protect access to resources than through the routes