[英]Rails multiple routes, same `as`
我有以下路線:
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
工作
您還可以將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
publisher_settings_path
已經返回了這三個路由的路徑,例如/admin/settings/general
。 因此,您無需多次設置as:
。
實際上,將其設置as:
對於多個路由多次將是沒有意義的,它必須返回一個值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.