Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 400
Majid Mushtaq Avatar asked Dec 09 '14 20:12

Majid Mushtaq


2 Answers

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

like image 90
mattforni Avatar answered Oct 20 '22 09:10

mattforni


For example we have orders which can be cancelled:

Following description in routes.rb

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

Will send request to 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

like image 5
itsnikolay Avatar answered Oct 20 '22 10:10

itsnikolay