Rails conditional ('if') statements based on controller action

后端 未结 4 1330
别跟我提以往
别跟我提以往 2021-01-31 16:59

There might be a better way to do this, but I\'m trying to make an if statement in rails, based on the current action, in a controller (this will be used in a view).

For

4条回答
  •  灰色年华
    2021-01-31 18:00

    You can do it this way:

    class ApplicationController < ActionController::Base
      layout :set_layout
    
      def set_layout
        case params[:action]
        when "foo"
          "foo_layout"
        when "bar"
          "bar_layout"
        ...
        else
          "default_layout"
        end
      end
    
      ...
    
    end
    

    hope it helps =)

提交回复
热议问题