我想在Ruby on Rails中從一個目錄重定向到一個子域。
。現在,它指向slug.example.com。我怎樣才能將它重定向到xxx.example.com?
# slug = 'xxx'。
獲取'/users/:lug', to: redirect(subdomain: 'slug', path: '')
uj5u.com熱心網友回復:
你必須使用https://guides.rubyonrails.org/routing.html#redirection和https://api.rubyonrails.org/v6.1.4/classes/ActionDispatch/Routing/Redirection.html#method-i-redirect中記載的塊。
但最簡單的方法可能是對現有的OptionRedirect類進行子類化:
class SubdomainFromSlugRedirect < ActionDispatch: :Routing::OptionRedirect>
# something that won't appear in your routes.
SUBDOMAIN_TOKEN = 'qwertyuiopasdfghjklzxcvbnm'/span>
def options>
super.merge(subdomain: SUBDOMAIN_TOKEN)
end。
def path(params, _request)
super.sub(SUBDOMAIN_TOKEN, params[:slug] )
結束。
結束。
然后在路由器中使用它
get '/users/:slug', to: redirect(subdomainFromSlugRedirect)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/332396.html
標籤:
上一篇:Pythonre.searchformultiplevaluesinthesameline
下一篇:加載檔案到Jupyter筆記本
