开发者

Rails routes for article/234 and article/index

I need to setup the following urls:

/article (article#index)
/articles/1234  ( article#show with id 1234)
/articles/new  (article#new)

Can开发者_高级运维 I define this using:

resources :article do
  ???
end


If we look very closely at your question, it appears that you want the index to be at /article instead of the default Rails REST convention, which is /articles

It doesn't make any apparent sense to model your routes that way, but if that is surely what you want to do, then you could add one more route line in addition to the call to resources

resources :articles
match '/article', :to => 'articles#index'


It sounds like you're just learning rails. I'd suggest generating an article scaffold. It will set up a route like so for you:

resources :article

And you'll get RESTful routes setup for you automagically by rails

GET        /articles            index     display a list of all articles
GET        /articles/new        new       return an HTML form for creating a new article
POST       /articles            create    create a new article
GET        /articles/:id        show      display a specific article
GET        /articles/:id/edit   edit      return an HTML form for editing an article
PUT        /articles/:id        update    update a specific article
DELETE     /articles/:id        destroy   delete a specific article

You can then dig into this and learn how rails does things.

Here's the official rails routing guide.


If you want those URLs and nothing else, you should put the following in routes.rb:

resources :article, :only => [:index, :show, :new]

In case you haven't stumbled upon the official Rails 3 routing guide, you should definitely take a look at it!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜