Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple layouts in ror

Just started Ruby on Rails yesterday. In my layouts/application.html.erb i have:

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag    "application", media: "all" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>  
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
    </div>
    <%= render 'layouts/footer' %>
  </body>
</html> 

Coming from php ->codeigniter background, i'm assuming render is similar to $this->load->view(''); in codeigniter.
Although this works fine, i would like to have multiple application layout files e.g

  1. layout/application default
  2. layout/application fullwidth(for fullwidth pages)
  3. and so on..

In codeigniter you simply declare which of the templates/layout files you wish to use but as ruby on rails is a bit magical(It does a lot of things for you), i'm assuming it calls the application layout by default. I was wondering if there's a way to choose what layout file i want?

like image 563
Skyalchemist Avatar asked Mar 05 '13 15:03

Skyalchemist


People also ask

How do you use nested layouts Rails?

Rails provides us great functionality for managing layouts in a web application. The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc.

What are layouts in Rails?

In Rails, layouts are pieces that fit together (for example header, footer, menus, etc) to make a complete view. An application may have as many layouts as you want. Rails use convention over configuration to automatically pair up layouts with respective controllers having same name.

How can you tell Rails to render without a layout?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.

What are partials in Rails?

A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts. For example, you might have a login form that you want to display on 10 different pages on your site.


2 Answers

@Deefour gave the right resources, here is a nice quick example on how you can implement this in Rails 4.

In a controller you can specify where you want to fetch what layout for a certain action and have very nicely fine grained control over which layout is used.

class PagesController < ApplicationController
  layout "fullwidth", except: [:index, :faqs]

  def popout
    # Render this action with specific layout
    render layout: "popout"
    #renders with views/layouts/popout.html.erb
  end

  def index
    #renders with views/layouts/application.html.erb
  end

  def about_us
    #renders with views/layouts/fullwidth.html.erb
  end

  def team
    #renders with views/layouts/fullwidth.html.erb
  end

  def logo
    #renders with views/layouts/fullwidth.html.erb
  end

  def faqs
    #renders with views/layouts/application.html.erb
  end
end

application.html.erb is the rails standard layout file. I assume it is present, and it is the default fallback!

like image 114
mahatmanich Avatar answered Oct 06 '22 13:10

mahatmanich


You're looking for the layout method.

This Rails Guide will help you, specifically Finding Layouts. I'd provide more detail here, but the previously mentioned documentation and guide provide more than enough examples and usage instruction.

like image 27
deefour Avatar answered Oct 06 '22 13:10

deefour