Two layouts for one controller

Hi all,

I am implementing the functionality of content management through
FCKeditor.
I have a controller called aboutus and the corresponding table in the
database for accessing the contents. Layouts of back end and front end
are different.
Now in the front end when i am trying to access aboutus page it is
showing that page but with the backend layout.
I want to show the about us page with the layout of front end.For this I
need some method so that I can access two layouts for a single
controller.Please tell me if there is any method to implement it.

Any help would be greatly appreciated.

Thanks,
Rohit.

On Nov 30, 2007, at 11:48 PM, Rohit M. wrote:

controller.Please tell me if there is any method to implement it.
In the controller add this line

layout :choose_layout

then add this method

def choose_layout
if action_name == ‘login’
return ‘2colwn’
else
return ‘1col’
end
end

Of course, change it for your action and layout names.


def gw
acts_as_n00b
writes_at(www.railsdev.ws)
end

You can also do
layout :some_layout, :only => [:new, :edit]
or
layout :some_other_layout, :except [:list, :show]

And of course at the end of your action you can just say
render :layout => ‘something_else’

Fred

Hi,

There is still a problem with the above code.
If I write in my code
layout => ‘store’, :only => [:sign_up]
layout => ‘login’, :except => [:sign_up]

then both of them together doesn’t work. Only one of them works at a
time, but I want store layout for sign_up page and login layout for all
other pages.

Please provide some solution.

Thanks,
Rohit.

Hi Rohit,

Here’s how I did it:

in application.rb

def choose_layout
if [‘welcome’].include?(action_name)
‘one_column’
elsif ![‘welcome_xml’, ‘math_xml’].include?(action_name)
‘two_column’
else
nil
end
end

then in the controller

before_filter :choose_layout

I put the choose_layout filter last as there’s no point in choosing a
layout if one of the other filters might fail. The local variable
action_name is actually a Rails provided global-ish variable, so you
don’t need to set it yourself. If you have to make a decision
between actions that are named the same in multiple controllers, you
will also need to evaluate which controller is active. I have not
required that yet, so I don’t know exactly how to do it. I would
like to think it’s as easy as controller_name (to go along with
action_name), but I’m only guessing. In my testing, it was important
to return nil if I didn’t want to use a layout, but I don’t have an
API reference or any other documentation to prove that.

You can just as easily do an "if action_name == " instead
of using include?. At one time I had more than one action that was
using the ‘one_column’ layout and when I changed it, I didn’t change
the conditional to a singleton test. I probably should as I expect a
simple equality test takes fewer processor cycles than include?.

Peace,
Phillip

Frederick C. wrote:

You can also do
layout :some_layout, :only => [:new, :edit]
or
layout :some_other_layout, :except [:list, :show]

And of course at the end of your action you can just say
render :layout => ‘something_else’

Fred

Hi,

Thanks a lot Fred. Its working now.

I made a typo in my earlier post. Instead of

before_filter :choose_layout

I should have written

layout :choose_layout

Sorry if this caused any confusion.

Peace,
Phillip

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs