Is there some sort of Master Page functionality in Ruby on Rails?
Asked Answered
P

2

8

I've been a .Net developer for the past seven years or so, and been working with ASP.Net for the last couple of years. I'm now using Ruby on Rails for some projects, and I'm wanting to know if there is something in Ruby on Rails that lets you do master page type stuff?

Basically, I want a way to provide a consistent look and feel for the site with a header and footer and so on, and then just have each page put its content inside of that. How do you accomplish this?

Peugia answered 23/10, 2009 at 16:5 Comment(5)
everyone else calls them "layouts"Ankylosaur
guides.rubyonrails.org/layouts_and_rendering.htmlAnkylosaur
@NSD I've searched and didn't find anything on master pages... now that I know they're called layouts outside of the .net environment, I'll go read about it... don't try to stereotype .net developers, there are a lot of very good onesPeugia
Thanks @Mauricio, if you put that on as an answer I'll accept it.Peugia
@NSD I see what you did there. BTW I'm a .net developer.Ankylosaur
A
6

in your rails project in app/layouts/application.(html.erb|html.haml), this is the layout or equivalent for master. You can also create other layouts and specify the layout to use for each action :

render :index, :layout => "awesome"

Or specify the layout for a whole controller :

class PostController < ActionController::Base
  layout "super_awesome"
end
Anastasiaanastasie answered 23/10, 2009 at 19:13 Comment(0)
A
0

You can use layout which is look like master page in ASP.Net, there are many way to assign master page to your rail application :

  1. For single page :
class EmployeesController < ApplicationController
  layout "submaster"
  # ---
end

submaster is located at app/views/layouts/submaster.html.erb

  1. For whole application :
class ApplicationController < ActionController::Base
  layout "main"
  #...
end

main is located at app/views/layouts/main.html.erb

  1. Layout at run-time :
class EmployeesController < ApplicationController
      layout :emp_layout

      def show
        # ---
      end

      private
        def emp_layout
          @current_user.admin? ? "admin" : "submaster"
        end

 end

If current user is a admin user, they'll get a admin layout else submaster layout.

If possible, please check yield identifies in rails.

Anticipate answered 17/7, 2015 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.