0

Is there a way to make a certain Rails view (e.g. index.html.haml) use a certain template? This is done in a controller like so:

layout 'my_layout'

Is it possible to require a layout directly in a view? Thanks.

beakr
  • 5,709
  • 11
  • 42
  • 66
  • 1
    [This question addresses your needs](http://stackoverflow.com/questions/3025784/rails-layouts-per-action) – rpedroso Jun 26 '12 at 02:38
  • That seems also to be setting the layout from the controller. – Jacob Mattison Jun 26 '12 at 02:51
  • Rails provides facilities to set the layout from the controller as standard procedure. It's possible that there may be some obscure way to set it from the view, but in keeping with MVC it's best that the controller make those decisions. That's how it's designed to work and I'd recommend not trying to subvert it. – Kevin Bedell Jun 26 '12 at 13:09
  • I'm trying to modify Devise's views which **do not** have a controller. – beakr Jun 26 '12 at 13:12

1 Answers1

0

According to the Rails Guide, Layouts and Rendering

"When Rails renders a view as a response, it does so by combining the view with the current layout"

So it doesn't seem as if it's possible to change the layout once the view has begun to be rendered.

Moreover, I'd propose that determining the layout is more of a controller function. If you have logic in your view that tries to determine the layout, I'd bet that logic might better be placed in the controller.

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114