简体   繁体   English

Rails 4 - 如何匹配命名空间中的路由

[英]Rails 4 - How to match routes in namespace

Hi i have admin panel controller and have many controller in admin panel. 嗨我有管理面板控制器,管理面板中有很多控制器。 I want to match routes usually without namespace i've used 我想匹配路线,通常没有我使用过的命名空间

match ':controller(/:action(/:id))', :via => [:get, :post]

I want this in namespace controller my current 我希望在命名空间控制器中使用此功能

router.rb router.rb

namespace :admin do

get '', to: 'dashboard#index', as: '/'

get 'dashboard/index'

##AUTHENTICATION
get 'login/index'
get 'login/logout'
post 'login/attempt_login'
get 'login/attempt_login'

##PAGES
get 'pages/index'
get 'pages/add_new'
get 'pages/edit'
post 'pages/create'
post 'pages/update'
post 'pages/task'
get 'pages/task'

##USERS
get 'users/index'
get 'users/edit'
get 'users/delete'
get 'users/destroy'
get 'users/update'
get 'users/add_new'
post 'users/create'
post 'users/update'
post 'users/task'

#USER GROUPS
get 'user_group/index'
get 'user_group/add_new'
get 'user_group/edit'
post 'user_group/create'
post 'user_group/update'
post 'user_group/task'

#USER GROUPS
get 'access_sections/index'
get 'access_sections/add_new'
post 'access_sections/create'
post 'access_sections/update'
post 'access_sections/task'

end

Any solution please? 有解决方案吗?

You simply wrap the routes you're declaring in a namespace like so: 您只需将您声明的路由包装在命名空间中,如下所示:

namespace :login do
   get 'index'
   get 'logout'
end

http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

For example we have orders which can be cancelled: 例如,我们有可以取消的订单:

Following description in routes.rb 以下是routes.rb描述

  resources :orders do
      post :cancel, to: 'orders/cancellations#cancel'
  end

Will send request to app/controllers/orders/cancellations_contoller.rb 将请求发送到app/controllers/orders/cancellations_contoller.rb

module Orders
  class CancellationsController
    def cancel
      @order = Order.find(params[:id]).cancel
    end
  end
end

It's useful to refactor controller resourses with many service methods. 使用许多服务方法重构控制器资源非常有用。

Wish it helps 希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM