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

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


By default, layouts/application.html.haml (.erb if you are not using haml).

In fact, layout file could be set per controller or per action, instead of per view, per view folder.

There are few cases:

To change the default layout file for all controller (ie. use another.html.haml instead of application.html.haml)

class ApplicationController < ActionController::Base  layout "another"  # another way  layout :another_by_method  private  def another_by_method    if current_user.nil?      "normal_layout"    else      "member_layout"    end  endend

To change all actions in a certain controller to use another layout file

class SessionsController < ActionController::Base  layout "sessions_layout"  # similar to the case in application controller, you could assign a method insteadend

To change an action to use other layout file

def my_action  if current_user.nil?    render :layout => "normal_layout"  else    render :action => "could_like_this", :layout => "member_layout"  endend


Yes, you can use different layouts and stylesheets within the same controllers.

The rails guide on layouts is a good place to start. Look at Section 3 - Structuring Layouts

There are several ways to use a different layout but one of the easiest is to simply add a file with the same name as your controller in the layouts/ folder. So if your controller is PostsController then adding a layouts/post.html.haml would cause rails to use that layout. If no such layout is found, and no other layouts are specified, rails will use the default of layouts/application.html.haml


If you do not want to go too complex, you can simply do this:

layout 'layout_one' def new   @user= User.new  render layout: 'landing_page'  end

this will do.