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

Ruby on-RailsLayout

Ruby on-Rails Problem Overview


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.

Our app was scaffolded and we then used nifty-generators to generate views, then added devise for authentication. We have views and controllers for two models: widgets and companies.

I currently have a single layout: layouts/application.html.haml, I don't see that referenced anywhere so I assume (a rails newbie) that it's always used by naming convention.

I now need to add a couple of views (for mobile browsers) which have a different stylesheet and layout (for example, no login/logout links in the top right), within the same controllers.

How can that be done?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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 - Ruby on-Rails

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 - Ruby on-Rails

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 - Ruby on-Rails

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 - Ruby on-Rails

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjpwView Question on Stackoverflow
Solution 1 - Ruby on-RailsPeterWongView Answer on Stackoverflow
Solution 2 - Ruby on-RailsDtyView Answer on Stackoverflow
Solution 3 - Ruby on-RailsLesly RevengeView Answer on Stackoverflow
Solution 4 - Ruby on-RailszechtzView Answer on Stackoverflow
Solution 5 - Ruby on-RailsdannymccView Answer on Stackoverflow