Merging Routes with Names in Rails

Given the following route.rb file:

# Add Admin section routes
 map.namespace :admin do |admin|
   admin.resources :admin_users
   admin.resources :admin_user_sessions, :as => :sessions
   admin.resources :dashboard

   # Authentication Elements
   admin.login '/login',  :controller => 'admin_user_sessions', :action => 'new'    
   admin.logout '/logout', :controller => 'admin_user_sessions', :action => 'destroy'

   # Default is login page for admin_users
   admin.root :controller => 'admin_user_sessions', :action => 'new'
end

Is it possible for the "admin" alias to be something else without having to change each redirect and link_ in the application? The main reason is that I would like to tune on the fly, and hopefully make it a little easier to guess.

+3
source share
3 answers

map.namespaceThe method simply sets some common route parameters inside its block. He uses the method with_options:

# File actionpack/lib/action_controller/routing/route_set.rb, line 47
        def namespace(name, options = {}, &block)
          if options[:namespace]
            with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
          else
            with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
          end
        end

Thus, instead, namespaceyou can use the method with_options:

map.with_options(:path_prefix => "yournewprefix", :name_prefix => "admin_", :namespace => "admin/" ) do |admin|  
  admin.resources :admin_users
  # ....
end

, , "yournewprefix" "admin"

admin_admin_users_path #=> /yournewprefix/admin_users
+7

(, api_version, ), :

#routes.rb
%w(v1 v2).each do |api_version|
  namespace api_version, api_version: api_version, module: :v1 do
    resources :some_resource
    #...
  end
end

, /v1/some_resource /v2/some_resource . params[:api_version], .

+4

, , , .

namespace :admin, :path => "myspace" do
  resources : notice 
    resources :article do 
      resources :links , :path => "url"
    end 
  end
end
+3

Source: https://habr.com/ru/post/1744199/


All Articles