Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails conditional layouts: Why does ":for" work as an option for the layout method?

In my controller I'm using layout 'application', :only => :edit

But I noticed that layout 'application', :for => :edit works as well.

Just curious because docs only mention :only & :except. I can't explicitly find :for in any docs for the layout method.

Rails v2.3 - layout (railsapi.com)

Rails v3.1 - layout (apidock.com)

like image 578
brandonjp Avatar asked Dec 12 '22 06:12

brandonjp


1 Answers

As I commented above, Rails is ignoring your :for parameter and should just be using the 'application' layout for all actions.

As far as rendering multiple layouts, it seems you can only have a single layout ... line for your controller; if you have multiples, it will only use the last one. And if the last one has an :only or :except parameter, it should fall back to the application layout for all the other actions. If it's not properly using the application layout and rendering without any layout whatsoever, make sure the previous developer didn't put something like layout nil somewhere in your controller/application.

If you want to handle multiple layouts inside your controller, you could also try this:

class UsersController < ApplicationController
  layout :choose_layout

  def choose_layout
    if action_name == "edit"
      "application"
    else
      "login"
    end
  end
end
like image 189
Dylan Markow Avatar answered Jan 05 '23 01:01

Dylan Markow