SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2000
    Location
    England
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails template switching help

    I've been playing with Rails for the past few days and I'm really getting into it after 6 years of PHP development, but I've hit my first major snag:

    I'm trying to introduce template/skin switching for an app and it seems to me that the simplest way is to change the value of ActionController::Base.template_root based on a method 'choose_skin' or the default of '/views'.As a quick test, I tried adding the following into application.rb (ApplicationController class):
    Code:
    	template_root = skin_choose || '/views'
     
     	def skin_choose
     		if @session[:skin]
     			skin = @session
     		end
     	end
    When I run the code, I get an 'Action Controller: Exception caught' error:
    Code:
    undefined local variable or method `skin_choose' for ApplicationController:Class
    I'm still very new to Rails so I'm probably missing a pretty obvious error to do with the scope, but I can't see it at the moment, so would appreciate any help getting the above to work (or suggesting a better method).

    Thanks in advance

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would seem that your template_root statement is in class scope but your skin_choose is an instance method (object scope).

    Try using a before filter.

    Code:
    # application.rb
    class ApplicationController < ActionController::Base
      before_filter :set_template_root
    
      private
        def set_template_root
           session[:skin] ||= '/views'
           @template_root = session[:skin]
        end
    end
    Not tested though!

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2000
    Location
    England
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Luke, that was certainly more along the right lines. The set_template_root method ended up looking like this:
    Code:
    	private
     	def set_template_root
     		session[:skin] ||= "#{RAILS_ROOT}/app/views/default"
     		self.class.template_root = session[:skin]
     		@template.base_path = template_root
     	end
    With a bit more searching, I found this blog post that had the answer.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •