Rails layouts per action? Rails layouts per action? ruby-on-rails ruby-on-rails

Rails layouts per action?


You can use a method to set the layout.

class MyController < ApplicationController  layout :resolve_layout  # ...  private  def resolve_layout    case action_name    when "new", "create"      "some_layout"    when "index"      "other_layout"    else      "application"    end  endend


If you are only selecting between two layouts, you can use :only:

class ProductsController < ApplicationController   layout "admin", only: [:new, :edit]end

or

class ProductsController < ApplicationController   layout "application", only: [:index]end


You can specify the layout for an individual action using respond_to:

  def foo    @model = Bar.first    respond_to do |format|      format.html {render :layout => 'application'}    end  end