2015-10-08 262 views
1

我正在嘗試提供用戶菜單的方法。作爲原型,我最終在routes.rb中使用了這種代碼。我想知道的是,還有更緊湊的方法來提供相同的效果而不會失去太多的清晰度?Rails-4自定義路由

get('ag', 
    :action   => :ag, 
    :controller  => 'navigation/main_menu') 

    get('menu/ag', 
    :action   => :ag, 
    :controller  => 'navigation/main_menu') 

    get('navigation/ag', 
    :action   => :ag, 
    :controller  => 'navigation/main_menu') 

    get('navigation/ag/journals', 
    :action   => :journals, 
    :controller  => 'navigation/ag/main_menu') 

    get('ap', 
    :action   => :ap, 
    :controller  => 'navigation/main_menu') 

    get('menu/ap', 
    :action   => :ap, 
    :controller  => 'navigation/main_menu') 

    get('navigation/ap', 
    :action   => :ap, 
    :controller  => 'navigation/main_menu') 

    get('menu/ap/journals', 
    :action   => :journals, 
    :controller  => 'navigation/ap/main_menu') 

    get('navigation/ap/journals', 
    :action   => :journals, 
    :controller  => 'navigation/ap/main_menu') 

    get('ar', 
    :action   => :ar, 
    :controller  => 'navigation/main_menu') 

    get('menu/ar', 
    :action   => :ar, 
    :controller  => 'navigation/main_menu') 

    get('navigation/ar', 
    :action   => :ar, 
    :controller  => 'navigation/main_menu') 

    get('menu/ar/journals', 
    :action   => :journals, 
    :controller  => 'navigation/ar/main_menu') 

    get('navigation/ar/journals', 
    :action   => :journals, 
    :controller  => 'navigation/ar/main_menu') 

    get('menu/currencies', 
    :action   => :index, 
    :controller  => 'navigation/ag/currencies_menu') 

回答

1

最起碼,您可以將您的所有路線,在模塊使用類似的控制器像這樣的:

controller 'navigation/main_menu' do 
    get 'ag', :action => :ag 
    get 'menu/ag', :action => :ag 
    get 'navigation/ag', :action => :ag 
    get 'ap', :action => :ap 
    get 'menu/ap', :action => :ap 
    get 'navigation/ap', :action => :ap 
    get 'ar', :action => :ar 
    get 'menu/ar', :action => :ar 
    get 'navigation/ar', :action => :ar 
end 

get('navigation/ag/journals', 
    :action   => :journals, 
    :controller  => 'navigation/ag/main_menu') 

controller 'navigation/ap/main_menu' do 
    get 'menu/ap/journals', :action => :journals 
    get 'navigation/ap/journals', :action => :journals 
end 

controller 'navigation/ar/main_menu' do 
    get 'menu/ar/journals', :action => :journals 
    get 'navigation/ar/journals', :action => :journals 
end 

get('menu/currencies', 
    :action   => :index, 
    :controller  => 'navigation/ag/currencies_menu')