簡體   English   中英

如何使用link_to轉到子域

[英]How to use link_to to go to a subdomain

我無法使用link_to轉到我的帖子/索引頁面。 我有以下link_to:

<%= link_to('NEWS', posts_path(subdomain:"news"),:class=>'navtext',:style=>"color:#bfbebc!important;")  %>

我的路線:

  resources :posts , constraints: { subdomain: 'news' }

當我位於http://news.lvh.me:3000/posts(link_to指向的實際頁面)時,鏈接有效,但是當我放置在任何其他頁面(例如root_path)上時,該鏈接無效。 我收到No route matches [GET] "/posts"錯誤,我認為這意味着該子域未在我的link_to中傳遞。 關於如何執行此操作的任何想法? 謝謝。

嘗試posts_url ,而不是posts_path

即使沒有(subdomain:"news")它也可以在我的項目上正常工作。 只需直接使用posts_url

如果沒有其他效果,則可以完全跳過幫助程序:

<%= link_to('NEWS', subdomain_link,:class=>'navtext',:style=>"color:#bfbebc!important;")  %>

為了使其在開發和生產中都能正常工作,您可以在application_helper.rb編寫一個輔助方法:

def subdomain_link
  Rails.env.development? ? '//whatever.localhost:3000' : '//news.whatever.com'
end

更進一步,您可以配置subdomain_link接受字符串作為參數並將其插值到域字符串中。 另一種方法是在config/environments/development.rbconfig/environments/production.rbconfig/environments/development.rb config變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM