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
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:
* In the application.rb (controller) do:
In fact if you need this for all controllers just throw the
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!
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