Rails 3路线:如何避免与“显示”动作发生冲突? [英] Rails 3 routes: How to avoid conflict with 'show' action?

查看:74
本文介绍了Rails 3路线:如何避免与“显示”动作发生冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前为张照片设置了以下路线

resources :photos
match 'photos/:user' => 'photos#user', :as => :user_photo
match 'photos/:user/:key' => 'photos#show', :as => :show_photo

那两条比赛路线给了我像这样的URL:

Those two match routes give me URLs like:

http://example.com/photos/joe_schmoe
http://example.com/photos/joe_schmoe/123xyz

...类似于Flickr格式化其URL的方式。

...similar to the way Flickr formats its URLs.

我遇到的问题是, photos /:user 路线要么被解释为 show 方法,或者,如果我将这些自定义路线放在资源之前,则像 / new 会被解释为 user 方法。

The problem I'm having, though, is that the photos/:user route is either interpreted as the show method, or if I put those custom routes before the resources then routes like /new get interpreted as the user method.

如何解决此问题而不必做很多事情自定义路线?

How can I get around that without having to do a ton of custom routes?

推荐答案

您需要将此自定义路线放在上方 resources:users ,以便在路由中首先对其进行匹配。

You'll want to put this custom route above the resources :users, so that it is matched first in the routes.

match 'photos/:user' => 'photos#user', :as => :user_photo
resources :photos

此外,您可以禁用显示动作:

Additionally you can disable the show action altogether:

resources :photos, :except => :show

这篇关于Rails 3路线:如何避免与“显示”动作发生冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆