Rails set layout from within a before_filter method

前端 未结 3 1133
闹比i
闹比i 2020-12-31 01:34

Is it possible to reset a default layout from within a before_filter method in Rails 3?

I have the following as my contacts_controller.rb:

cla         


        
3条回答
  •  伪装坚强ぢ
    2020-12-31 02:08

    If for some reason you cannot modify the existing controller and/or just want to do this in a before filter you can use self.class.layout :special here is an example:

    class ProductsController < ApplicationController
      layout :products
      before_filter :set_special_layout
    
      private
    
      def set_special_layout
        self.class.layout :special if @current_user.special?
      end
    end
    

    It's just another way to do essential the same thing. More options make for happier programmers!!

提交回复
热议问题