如何在路由上只写一次path_names,并为Rails 4.1中的所有路由提供服务?

时间:2014-11-04 11:31:04

标签: ruby ruby-on-rails-4 routes pathname

我想将我的path_names翻译一次,然后将其用于routes.rb中的所有路径。

这是我的routes.rb现在:

PATH_NAMES = { new: 'novo', edit: 'editar', show: 'ver' }

Rails.application.routes.draw do

  devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks',
                                    registrations: 'registrations' }

  resources :movements, :path => 'fluxo-de-caixa', path_names: PATH_NAMES
  resources :movement_categories, :path => '/fluxo-de-caixa/categorias', path_names: PATH_NAMES
  resources :installments, :path => 'parcelas', path_names: PATH_NAMES
  resources :contracts, :path => 'contratos', path_names: PATH_NAMES
  resources :accounts, :path => 'contas', path_names: PATH_NAMES
  resources :events, :path => 'eventos', path_names: PATH_NAMES
  resources :users, :path => 'usuarios', path_names: PATH_NAMES
  resources :spectator, :path => 'espectadores', path_names: PATH_NAMES

  devise_scope :user do
    get '/cadastrar' => 'devise/registrations#new'
    get '/entrar' => 'devise/sessions#new'
    get '/editar' => 'devise/registrations#edit'
    delete '/sair' => 'devise/sessions#destroy'
  end

  root 'home#index'
end

我希望你帮助我!!

我的问候! ; d

1 个答案:

答案 0 :(得分:4)

使用scope

scope(path_names: { new: 'novo', edit: 'editar', show: 'ver' }) do
  resources :movements, :path => 'fluxo-de-caixa'
  resources :movement_categories, :path => '/fluxo-de-caixa/categorias'
  resources :installments, :path => 'parcelas'
  resources :contracts, :path => 'contratos'
  resources :accounts, :path => 'contas'
  resources :events, :path => 'eventos'
  resources :users, :path => 'usuarios'
  resources :spectator, :path => 'espectadores'
end