如何在另一个控制器范围内制作控制器?

时间:2015-12-23 13:53:54

标签: ruby-on-rails ruby ruby-on-rails-4

我有一个名为status的控制器,其中包含许多方法,例如firstsecond等等。到目前为止。

所以,我有一个routes.rb文件,如下所示:

scope 'status', controller: :status do
  get 'first'
  get 'second'
  # and so on
end

我的路径应该看起来像'/ status / states':

scope :status do
  resources :states
end

如何将这些范围合并为一个以使用DRY&干净的代码?

1 个答案:

答案 0 :(得分:3)

  resource :status, only: [], controller: :status do
    resources :states
    collection do
      get 'first'
      get 'second'
      get 'third'
    end
  end

会给你这个:

       status_states GET    /status/states(.:format)          states#index
                     POST   /status/states(.:format)          states#create
    new_status_state GET    /status/states/new(.:format)      states#new
   edit_status_state GET    /status/states/:id/edit(.:format) states#edit
        status_state GET    /status/states/:id(.:format)      states#show
                     PATCH  /status/states/:id(.:format)      states#update
                     PUT    /status/states/:id(.:format)      states#update
                     DELETE /status/states/:id(.:format)      states#destroy
        first_status GET    /status/first(.:format)           status#first
       second_status GET    /status/second(.:format)          status#second
        third_status GET    /status/third(.:format)           status#third