我如何才能在 Rails4 中的特定子域 URL 上显示文本?
How could I display text only on a particular subdomain URL in Rails4?
我有一个多租户 Rails 4 应用程序。我想在测试人员登录其 'test' 子域帐户之前向他们显示一些说明。
因此,文本应该只针对特定的 URL 显示,而不会针对其他子域显示。我怎么能这样做?
例如以下是不正确的,但我想实现的一个例子:
#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 %>
您可以使用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 %>
您可以为该子域使用不同的布局。
在你的ApplicationController
中:
layout :set_layout
private
def set_layout
request.subdomain == 'test' ? 'test' : 'public'
end
并且在布局中,您可以在 <%= yield %>
之前添加更多文本
您可以在此处阅读有关布局的更多信息:http://guides.rubyonrails.org/layouts_and_rendering.html
我有一个多租户 Rails 4 应用程序。我想在测试人员登录其 'test' 子域帐户之前向他们显示一些说明。 因此,文本应该只针对特定的 URL 显示,而不会针对其他子域显示。我怎么能这样做?
例如以下是不正确的,但我想实现的一个例子:
#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 %>
您可以使用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 %>
您可以为该子域使用不同的布局。
在你的ApplicationController
中:
layout :set_layout
private
def set_layout
request.subdomain == 'test' ? 'test' : 'public'
end
并且在布局中,您可以在 <%= yield %>
您可以在此处阅读有关布局的更多信息:http://guides.rubyonrails.org/layouts_and_rendering.html