我有以下路线:
authenticated :user do
scope module: 'admin', path: ':publisher_id' do
get 'settings/general', to: 'publishers#index', as: 'publisher_settings'
post 'settings/general', to: 'publishers#create'
put 'settings/general', to: 'publishers#update'
end
end
但是第一个as:
只能使用一次。我该如何将其应用于所有这些路线?我希望他们所有人都为publisher_settings_path
答案 0 :(得分:0)
您还可以将as
选项传递给Rails Routes中的名称空间和范围。
# prefix the routing helper name: +sekret_posts_path+ instead of +posts_path+
scope as: "sekret" do
resources :posts
end
来源:https://api.rubyonrails.org/v5.1/classes/ActionDispatch/Routing/Mapper/Scoping.html
虽然我认为这也应该起作用
Rails.application.routes.draw do
resources :publishers, only: [:create, :update, :index], path: 'settings/general', as: 'publisher_settings'
end
得到我
publisher_settings GET /settings/general(.:format) publishers#index
POST /settings/general(.:format) publishers#create
publisher_setting PATCH /settings/general/:id(.:format) publishers#update
PUT /settings/general/:id(.:format) publishers#update
答案 1 :(得分:0)
publisher_settings_path
已经返回了这三个路由的路径,例如/admin/settings/general
。因此,您无需多次设置as:
。
实际上为多个路由多次设置相同的as:
是没有意义的,它必须返回一个值。