开发者

How do I modify the generated routes for a resource in Rails 3

In essence, I want to to be able to have URLs like these.

/articles
/articles/show/(art开发者_JAVA技巧icle id)
/articles/new
/articles/edit/(article id)

...etc. I want to use the resources :articles route but this generates routes that are different.

/articles
/articles/(article id)/show
/articles/new
/articles/(article id)/edit/

In fact, this is what I get when I run rake routes

articles     GET    /articles(.:format)          {:action=>"index", :controller=>"articles"}
articles     POST   /articles(.:format)          {:action=>"create", :controller=>"articles"}
new_article  GET    /articles/new(.:format)      {:action=>"new", :controller=>"articles"}
edit_article GET    /articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article      GET    /articles/:id(.:format)      {:action=>"show", :controller=>"articles"}
article      PUT    /articles/:id(.:format)      {:action=>"update", :controller=>"articles"}
article      DELETE /articles/:id(.:format)      {:action=>"destroy", :controller=>"articles"}

I am trying to avoid using this to get the behavior that I want.

match ':controller(/:action(/:id(.:format)))'

Because it the app would still respond to the above routes. Is there a simple way to change the routes to get the desired behavior? And if so how? If not then what should I do so that the app only responds to the desired URL structure?


Just set up match routing for the specific controller methods you need to set it up for:

match 'articles/show/:id' => 'articles#show', :via => :get, :as => 'article'
#etc etc ...

And then include the resources for the ones you don't need to change

resources :articles, :only => [:index, :new]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜