Forum: Ruby on Rails Using a different layout for just one action in controller

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-05-02 19:38
I have a controller where everything uses the same layout except for one
action called view.  I tried this at the top of the controller:

 layout "remote", :only => [:view]
 layout "standard", :except => [:view]

This renders standard for :view, and the other actions don't use a
layout at all!

How can I get what I want here?

83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-05-02 19:51
(Received via mailing list)
in your action:

render [render stuff], :layout => 'layout_name'

28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-05-02 20:00
Jason Roelofs wrote:
> in your action:
> render [render stuff], :layout => 'layout_name'
> Jason

I tried that, but my controller does checking on db stuff, and if it
fails, I do a redirect_to, then I get an error about render and
redirect_to in the same action.
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-05-02 20:07
(Received via mailing list)
In the controller:

layout :layout_a, :except => :fred
layout :layout_b, :only => fred

28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-05-02 20:29
MichaelLatta wrote:
> In the controller:
> layout :layout_a, :except => :fred
> layout :layout_b, :only => fred
> Michael

I tried that too, and got the result I mentioned in my first post.  I
tried it without giving an array, as well.
8bc543795b502900b5333aea73ad5533?d=identicon&s=25 Eden Li (edenli)
on 2007-05-03 04:41
(Received via mailing list)
Ugly, but use the method version of layout:

  layout :standard_or_remote
  def standard_or_remote(controller)
    if controller.action_name == 'view'
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-05-03 06:50
(Received via mailing list)
Jason wrote:
You can always put the test in an if/else...

28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-05-03 15:15
>   layout :standard_or_remote
>   def standard_or_remote(controller)
>     if controller.action_name == 'view'
>       'remote'
>     else
>       'standard'
>     end
>   end

I think that would work great.  I decided yesterday to move the view
action to a different controller that fit it better logically anyway.

Thanks everyone for your help!
1df9fc8ddf084661265bbae74a8d0b43?d=identicon&s=25 jko170 (Guest)
on 2007-06-18 20:39
(Received via mailing list)
Don't forget:

def standard_or_remote(controller)
  controller.action_name == 'view' ? 'remote' : 'standard'
This topic is locked and can not be replied to.