Home > database >  What will be the url help in scope and namespace in rails?
What will be the url help in scope and namespace in rails?

Time:04-29

Please tell me what will be the url helpers for the following code?

scope module: 'admin' do
  resources :articles, :comments
end

and

scope '/admin' do
  resources :articles, :comments
end

and

namespace :admin do
  resources :articles, :comments
end

CodePudding user response:

As per the rails guide document here - Controller namespace and routing

1.

If you want to route /articles (without the prefix /admin) to Admin::ArticlesController, you can specify the module with a scope block

the path will be like

GET articles_path   #index action
GET comments_path   #index action

If instead, you want to route /admin/articles to ArticlesController (without the Admin:: module prefix), you can specify the path with a scope block:

this will give the following path but the controller will contain Admin:: prefix

GET admin_articles_path  # index action
GET admin_comments_path   #index action
  1. with namespace, the route will prefix by admin as well as controller needs to have Admin:: module prefix
GET admin_articles_path  # index action
GET admin_comments_path   #index action

CodePudding user response:

Running the following commands on a console will return the available routes for your application.

rails routes | grep article
rails routes | grep comment
  • Related