rails 3, how add a view that does not use same layout as rest of app?

前端 未结 5 1053
醉话见心
醉话见心 2020-12-07 08:30

I could not find any docs or examples on how to structure my app to allow different views in the same controller to use completely different layouts and stylesheets.

5条回答
  •  时光说笑
    2020-12-07 09:00

    I am sure there's plenty of answers to this but here's another one where you can use different layouts per controllers or per action.

    class ListingsController < ApplicationController
      # every action will use the layout template app/views/layouts/listing_single.html.erb
      layout 'listing_single'
      # the 'list' action will use the layout set in the 'alternative_layout' method
      # you can also add multiple actions to use a different layout,just do like layout :alternative_layout, only: [:list,:another_action]
      layout :alternative_layout, :only => :list
    
       def show
       end   
    
       def list
       end
    
       private
       def alternative_layout
        if current_user.is_super_admin?
           #if current use is super admin then use the layout found in app/views/layouts/admin.html.erb otherwise use the layout template in app/views/layouts/listing_list.html.erb
          'admin'
        else
          'listing_list'
        end
      end
    end
    

提交回复
热议问题