Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Set an ActionController's template root on each request (See related posts)

Set an ActionController's template root on each request for a domain acts as account key pattern. Careful with this code though. It relies on the behavior of a private rails method and could change 50 times in the next week. (see my blog post)

   1  class MyController < ActionController::Base
   2    before_filter :set_site_template_root
   3    def set_site_template_root
   4      self.class.template_root = "#{RAILS_ROOT}/#{app}/#{views}/#{current_site.domain}")
   5      @template.base_path = template_root
   6    end
   7  end


   1  module ActionView
   2    class Base
   3      private
   4      alias_method :rails_assign_method_name, :assign_method_name
   5  
   6      # add a domain prefix at the end
   7      def assign_method_name(extension, template, file_name)
   8        rails_method_name = rails_assign_method_name(extension, template, file_name)
   9        @@method_names[file_name || template] = respond_to?(:current_site) ?
  10          "#{rails_method_name}_#{current_site.domain}".intern :
  11          rails_method_name
  12      end
  13    end
  14  end

You need to create an account or log in to post comments to this site.


Click here to browse all 5827 code snippets

Related Posts