how to set up and use a Rails routes prefix

后端 未结 7 922
北海茫月
北海茫月 2021-02-05 07:39

When running rake routes I see:

 Prefix   Verb   URI Pattern                                       Controller#Action
 articles  GET    /articles(.:format)               


        
7条回答
  •  鱼传尺愫
    2021-02-05 08:00

    The prefix can be used with the route helpers to generate routes in your application. So in your application, the articles_path helper generates a route for articles index page, new_article_path is the route for the new article page, and article_path(@article) would be the route for the show page for @article.

    To specify a different route, you could change your routes.rb file to:

    resources :articles, as: :documents
    

    That will give you:

    documents  GET    /articles(.:format)                               articles#index
    new_document GET    /articles/new(.:format)                           articles#new
    

    I might consider changing the resource name to documents as it can get confusing when you rename routes and get a mix of terminology between the route and the controller.

提交回复
热议问题