rubyTrials

A way to keep track of breakthroughs I've made in Ruby and Ruby on Rails.

My Photo
Name:
Location: Cadillac, Michigan, United States

Sunday, May 14, 2006

conditional layouts

I don't know about you, but I always make a admin tool for all my rails projects. Being generally lazy I don't like making new controllers just for the admin, but I do make a different layout. How then to get the controller to display the right layout based on which layout the response came from?

I found the easiest way was to pass a

:layout => "admin"

parameter in the routes for each action I wanted to apply the admin layout to. That's what routes are for right?

Anyways, to make this magic happen it takes code in two separate spots.

* In the controller in question do:

layout :get_layout


* In the application.rb (controller) do:

private
def get_layout
if params[:layout]
return params[:layout]
else
return "application"
end
end


In fact if you need this for all controllers just throw the

layout :get_layout

right in the application.rb too. I love inheritance! Don't you?

Now you have the ability to do conditional layouts to your hearts content!

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home