Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails two different layout templates in same controller?

I have a PagesController with a layout 'pages.html.erb' specified.

  class PagesController < Spree::StoreController
    layout 'pages'
    respond_to :html

    def lifestyle
      render 'lifestyle'
    end

    def co_step_1
      render 'co_step_1'
    end

    def co_step_2
      render 'co_step_2'
    end

  end

Is it possible to have an additional method in PagesController that uses a different layout? In other words i want to override the layout 'pages.html.erb' in an additional method.

like image 317
StandardNerd Avatar asked Dec 10 '22 17:12

StandardNerd


1 Answers

A bit different answer from the others. No need for before actions or similar, just use a layout and method to distinguish which layout to use, like:

class PagesController < Spree::StoreController

    layout :resolve_layout
    respond_to :html

    def lifestyle
      render 'lifestyle'
    end

    def co_step_1
      render 'co_step_1'
    end

    def co_step_2
      render 'co_step_2'
    end

   private

   def resolve_layout
     action_name == 'pages' ? 'pages' : 'custom_layout'
   end

end

Or whatever logic you want to use to decide which layout to use.

like image 150
Aleks Avatar answered Dec 31 '22 03:12

Aleks