MasterPage equivalent in Ruby on Rails, Trying to define a site wide layout MasterPage equivalent in Ruby on Rails, Trying to define a site wide layout WebForms and MVC has a concept of Masterpages which make it easy to define a one time layout for all the page of your site. In Rails I'm struggling to find an equivalent usage pattern or feature.

From what I've read it's really easy to define a layout in every action with:

layout: 'viewname' 

Now that seemed pretty ceremonial to include in every controller so I added:

layout: 'application' 

in the base ApplicationController.

So far this is working ok unless I have a more specific layout in the view pages. Is this common technique for getting a consistent style in your Rails application?

Check Checkbox based on value in CSV


linq to sql joining two table
Imagine a simplified blog where i have a controller called PostsController which has two actions: index and show. Inserting markup into hidden jqGrid column The index action is called when the user hits - this action displays all of the available blog posts.. MVC make action link perform a submit The show action is called when a user receive s a specific blog article - i.e. mvc int property bind exception thing-interesting. How do I pass a list of integers to an MVC action? Let's say this i want the index page to have a two column layout and i want the show page for each blog-article to have a three column layout. form post/anti forgery To achieve this, i would simply define two separate layouts (in app/views/layouts folder) - we'll call the two column layout "application" and we'll call the three-column layout "alternate".. MVC - Changing or adding a class for either 'Html.ValidationMessage' or 'Html.TextBox' In rule to receive the index page to use the two-column layout and the show page to use the three-column layout, i could just did the following in our controller:.
class PostsController < ApplicationController   def index     @posts = Post.all     render :layout => "application"   end    def show     @post = Post.find(params[:id])     render :layout => "alternate"   end end 
If i want all actions to use the same layout, i must just did this:.
class PostsController < ApplicationController   layout "application"    def index     @posts = Post.all   end    def show     @post = Post.find(params[:id])   end end 
Finally, if i did not specify which layout i want to use, then Rails will by default, render any layout which has the same name as the resource i are displaying. So in our example, where our resources are called "Posts", if i define a third layout called posts.html.erb (in app/views/layouts) then Rails will automatically use this layout when the user renders any of the actions in the PostsController - providing of course this i have not explicitly asked Rails to render ananother layout..... Hope it helps,.


This PDF book excerpt from Rails for .Net Developers has a pretty good explanation of Rails layouts, along with a comparison to ASP.Net MasterPages. Since it seems to job pretty well, it's probably used fairly often, at least by developers familiar with the master page concept..

38 out of 100 based on 18 user ratings 618 reviews