Layout differenti per lo stesso controller
Può succedere di dover utilizzare template grafici diversi per metodi differenti di uno stesso controller. In prima analisi verrebbe da implementare una soluzione che prevede una chiamata esplicita al layout da utilizzare all’interno di ogni singola action.
Qualcosa del tipo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class PostController < ApplicationController def index [...] :layout => 'standard' end def show [...] :layout => 'standard' end def new [...] :layout => 'admin' end def edit [...] :layout => 'admin' end end |
Esiste però un modo più semplice e DRY di implementare la stessa soluzione. Per selezionare il layout corretto da utilizzare Rails cerca una action chiamata “choose_layout”.
Definendo la propria versione di “choose_layout” è possibile gestire banalmente la scelta del layout come potete vedere nel codice che segue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class PostController < ApplicationController layout :choose_layout def index [...] end def show [...] end def new [...] end def edit [...] end private def choose_layout if [ 'new', 'edit' ].include? action_name 'admin' else 'standard' end end end |
Questa action utilizza la variabile Rails action_name che contiene il nome dell’action corrente per decidere quale layout caricare.
Quindi se action_name vale ‘new’ o ‘edit’ verrà caricato il layout ‘admin‘, altrimenti per qualsiasi altra action verrà caricato il layout ‘standard‘.
Tags: layout, rails, ruby on rails

















[...] This post was mentioned on Twitter by Jason Noble, DevInterface. DevInterface said: "Different layout for the same controllerLayout differenti per lo stesso controller" – http://bit.ly/d0P7ZU #rubytweets [...]
The layout call already supports an
nly and :except parameter, for exactly this purpose. Why reinvent the wheel?
What Stephen said, also in this example you wouldn’t even need to specify the layout if you separate the admin features into it’s own controller. In my experience applications that try to combine user and admin functionality in the same controllers are over complicated and poorly implemented.
Sometimes the logic behind which layout to render is more complicated than, “This action uses this template and this action uses that one”
I hadn’t seen a symbol passed to the layout method, thanks for sharing!
@Stephan: I’m agree with you we don’t have to reinvent the wheel.
But I think this method can be helpful if you have to add some conditions.
Using this approach you can write something like:
2
3
4
5
6
7
if some_conditions and [ 'new', 'edit' ].include? action_name
'admin'
else
'standard'
end
end
@Jamie: the ‘admin’ name for the layout in my example is just for demonstration purpose, I don’t mean that the same controller will combine user and administration functionality. It’s up to developer to implement controllers as he prefers.