开发者

Difference between :as option in Rails 2 and Rails3 routing?

In Rails 2.X we have:

 map.resources :posts, :controller => 'posts', :as => 'articles'

This essentially creates an alias for our posts routes. For example, this sends "domain.com/articles/" to the posts controller index action.

In Rails3, however, the :as option behaves differently. For example:

 resources :posts, :controller => 'posts', :as => 'article开发者_如何学运维s'

sets a named route rather than an alias, and going to "domain.com/articles/" gives an error:

No route matches {:controller=>"posts"}

How do I get the old (Rails 2) :as behavior using the new (Rails 3) api?

PS: Please don't tell me to simply rename my controller. That's not an option for me.


From some cursory reading of the RoR guide on routing, I think you might need to try:

resources :articles, :controller => "posts"

(http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use)

You might also need to add :as => "articles", but that named helper might already be set up since you are adding :articles resources.


You can accomplish this same behavior using the path option:

resources :posts, :path => '/articles/'

Now for example /posts/new becomes /articles/new.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜