If you want to set a controller to utilize a specific layout for all its actions Notice its
layout 'admin'
instead of
layout :admin
since passing a symbol does something different.
class UsersController < ApplicationController layout 'admin' end
So lets say you wanted to only apply layout to specific actions maybe you think you could do this because you can do this with before_actions:
class UsersController < ApplicationController layout 'form', only: %w{new create edit update} end
This, however, doesn't work, so how do you get conditional layouts? This is where symbol comes in, its actually telling it to call a function in the controller to handle the logic
class UsersController < ApplicationController layout :guess def guess case action_name when "new", "create", "edit", "update" "form" else "application" end end end
Also you noticed when I did this:
%w{new create edit update}
This is a clever shorthand for arrays which will split on the space so its the same as:
['new', 'create' , 'edit' , 'update']
I keep thinking about making an open-source Rails course, can you tell?
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If you want to set a controller to utilize a specific layout for all its actions
Notice its
instead of
since passing a symbol does something different.
So lets say you wanted to only apply layout to specific actions maybe you think you could do this because you can do this with before_actions:
This, however, doesn't work, so how do you get conditional layouts? This is where symbol comes in, its actually telling it to call a function in the controller to handle the logic
Also you noticed when I did this:
This is a clever shorthand for arrays which will split on the space so its the same as:
I keep thinking about making an open-source Rails course, can you tell?