如何仅在Rails4中的特定子域URL上显示文本?

时间:2015-01-23 09:50:38

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

我有一个多租户Rails 4应用程序。我想在测试人员登录他们的“测试”之前显示一些说明。子域帐户。 因此,文本应仅针对该特定URL显示,但不应针对其他子域显示。我怎么能这样做?

E.g。以下是不正确的,但我想要实现的一个例子:

#in devise/sessions/new.html.erb 

<% if url == 'http://test.lvh.me:3000/users/sign_in' %>
  <p>Some text displayed here, just for this subdomain...</p>
  <h2>Sign in</h2>
  ...
<% else %>
  <h2>Sign in</h2>
  ...
<% end %>

2 个答案:

答案 0 :(得分:3)

您可以使用request.subdomain

<% if request.subdomain == 'test' %>
  <p>Some text displayed here, just for this subdomain...</p>
  <h2>Sign in</h2>
  ...
<% else %>
  <h2>Sign in</h2>
  ...
<% end %>

答案 1 :(得分:1)

您可以为该子域使用不同的布局。

ApplicationController

  layout :set_layout

  private

  def set_layout
    request.subdomain == 'test' ? 'test' : 'public'
  end

在布局中,您可以在<%= yield %>

之前添加更多文字

您可以在此处详细了解布局:http://guides.rubyonrails.org/layouts_and_rendering.html