User Variable in Rails view filename

By default, Rails can find views with the format, language, and language of the template in the file name (so I can create index.de.json.erb)

Can I add another custom parameter to the view file name?

I would like to transfer the current subdomain, so it http://foo.example.com/will display index.foo.html.erb, and it http://bar.example.com/will display index.bar.html.erb(both of them with a index.html.erbbackup).

+5
source share
2 answers

, , , ActionView::LookupContext. (subdomain) LookupContext. :

ActionView::LookupContext.register_detail(:subdomain) do
  ['default_subdomain']
end

LookupContext subdomain, . . ActionView::FileSystemResolver , : :

ActionController::Base.view_paths = ActionView::FileSystemResolver.new(
  Rails.root.join('app', 'views'),
  ':prefix/:action{.:locale,}{.:subdomain,}{.:formats,}{.:handlers,}'
)

Dir.glob ( :*). glob {.:subdomain,} " .:subdomain, ", , .

- ApplicationController, LookupContext:

class ApplicationController < ActionController::Base
  def details_for_lookup
    {:subdomain => [request.subdomain]}
  end
end

( , . Rails 3.2.5)

+8

, . , , .

, - :

# in ApplicationController:
  before_filter :set_view_paths

  def set_view_paths
    self.class.view_paths = Rails.root.join('app', 'views', controller_name, request.subdomain)
  end

foo foo views/controller_name.

append/prepend_view_path , .

+1

All Articles