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

Solution 1:

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
  end
end

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 instead
end

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"
  end
end

Solution 2:

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

Solution 3:

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.

Solution 4:

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

Solution 5:

Well, if it's a different view for mobile devices but all desktop versions are the same then you could use JQtouch.

http://railscasts.com/episodes/199-mobile-devices

# config/initializers/mime_types.rb
Mime::Type.register_alias "text/html", :mobile

# application_controller.rb
before_filter :prepare_for_mobile

private

def mobile_device?
  if session[:mobile_param]
    session[:mobile_param] == "1"
  else
    request.user_agent =~ /Mobile|webOS/
  end
end
helper_method :mobile_device?

def prepare_for_mobile
  session[:mobile_param] = params[:mobile] if params[:mobile]
  request.format = :mobile if mobile_device?
end

The above code is taken from the Railscasts example.