Rails-4自定义路由

时间:2015-10-08 13:58:08

标签: ruby-on-rails ruby-on-rails-4 rails-routing

我正在尝试提供用户菜单的方法。作为原型,我在routes.rb中得到了这种代码。我想知道的是,有一种更紧凑的方法可以提供相同的效果而不会失去太多的清晰度吗?

  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 个答案:

答案 0 :(得分: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' )