Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a layout for a specific action

If I would like to use a layout for a certain action (say, the show action) that is different from the layout declared at the top of the controller.rb file, how could I do this? This must be possible in rails, but I cannot seem to find anything about it.

like image 547
providence Avatar asked Apr 02 '11 18:04

providence


2 Answers

  render :layout => 'otherlayout' 
like image 151
Don Roby Avatar answered Oct 04 '22 11:10

Don Roby


layout 'layout', :only => [:first_action, :second_action] layout 'second_layout', :only => [:third_action, :fourth_action] 

Don's is right as well, just depends on your application which is more DRY (or DRY-er?)


EDIT My previous code is mistaken. You cannot specify the layout function multiple times. I found this solution online for dynamic layout rendering:

class OrdersController < BaseController   layout :determine_layout  private   def determine_layout     %w(new).include?(action_name) ? "some_layout" : "public"   end end 

Source: apidock.com/rails/Actio...

like image 38
Kyle Macey Avatar answered Oct 04 '22 11:10

Kyle Macey