如何为路由添加前缀? Rails(4.2.5)路由混乱。

时间:2016-01-19 22:34:59

标签: ruby-on-rails routing

我有两个控制器offersposts

routes.rb中,我得到了以下内容......

resources :offers

get "/posts"          => "posts#index"
post "/posts"         => "posts#create"
get "/posts/new"      => "posts#new"
get "/posts/:id/edit" => "posts#edit"
get "/posts/:id"      => "posts#show"
put "/posts/:id"      => "posts#update"  
patch "/posts/:id"    => "posts#update"   
delete "/posts/:id"   => "posts#destroy"

据我了解,这两种路由方式在操作上是相同的。或者换句话说resources :offers是写出每条路线的正确捷径。

我的问题是当我做rake routes时,我得到了......

    Prefix Verb   URI Pattern                Controller#Action
    offers GET    /offers(.:format)          offers#index
           POST   /offers(.:format)          offers#create
 new_offer GET    /offers/new(.:format)      offers#new
edit_offer GET    /offers/:id/edit(.:format) offers#edit
     offer GET    /offers/:id(.:format)      offers#show
           PATCH  /offers/:id(.:format)      offers#update
           PUT    /offers/:id(.:format)      offers#update
           DELETE /offers/:id(.:format)      offers#destroy
     posts GET    /posts(.:format)           posts#index
           POST   /posts(.:format)           posts#create
 posts_new GET    /posts/new(.:format)       posts#new
           GET    /posts/:id/edit(.:format)  posts#edit
           GET    /posts/:id(.:format)       posts#show
           PUT    /posts/:id(.:format)       posts#update
           PATCH  /posts/:id(.:format)       posts#update
           DELETE /posts/:id(.:format)       posts#destroy

'简写' offers路由分配了四个前缀,而速记posts路由只有两个。

所以我的问题是: 为什么所有的路线都不会在写出来时得到前缀?

有什么方法可以在写出路线时为路线分配前缀吗?

1 个答案:

答案 0 :(得分:0)

通过执行以下操作,可以为路线分配前缀:

root 'pages#index', as: :home,其中:home是您的前缀。

来源: Generating Paths and URLs from Code