Rails layout

Bonjour,

J’utilise dans un même controller deux layouts différents.

J’aimerais donc faire, d’après la doc,

layout ‘public’, :only => [:new, :create, :forgot_password]
layout ‘home’, :only => [:show, :edit]

Mais cela ne marche pas. En effet, seule la dernière ligne est prise
en compte.

Y a-t-il une solution pour utiliser deux layouts différents dans un
même controller ?

Je vous remercie par avance,

Pierre V.

Bonjour,

Je n’ai jamais testé mails il me semble que tu peux executer une
methode pour deterniner ton layout du style

layout :quel_layout

def quel_layout
if condition
‘public’
else
‘home’
end
end

Il faut que tu trouves la condition qui différencie l’action dans
laquelle tu te trouves

Oui, c’est la solution de dernier recours que je voulais pas
utiliser :slight_smile:

Tiens, au fait, qu’en dit la doc ?..

Conditional layouts

If you have a
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621that
by default is applied to all the actions of a controller, you still
have the option of rendering a given action or set of actions without a
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621,
or restricting a
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621to
only a single action or a set of actions. The
:only and :except options can be passed to the
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621call.
For example:

class WeblogController < ActionController::Base
layout “weblog_standard”, :except => :rss

# ...

end

This will assign “weblog_standard” as the WeblogController’s
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621except
for the
rss action, which will not wrap a
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621around
the rendered view.

Both the :only and :except condition can accept an arbitrary number of
method references, so #:except => [ :rss, :text_only ] is valid, as is
:except
=> :rss.
Using a different
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621in
the action render call

If most of your actions use the same
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621,
it makes perfect sense to define a controller-wide
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621as
described above. Sometimes you’ll have exceptions where one action
wants
to use a different
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621than
the rest of the controller. You can do this by passing a
:layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621option
to the
render call. For example:

class WeblogController < ActionController::Base
layout “weblog_standard”

def help
  render :action => "help", :layout => "help"
end

end

This will render the help action with the “help”
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621instead
of the controller-wide “weblog_standard”
layouthttp://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html#M000621.


Michel B.

Ca dépend de ce que tu veux dire par “simplement”. Indiquer dans le
render
de l’action quel layout utiliser, je trouve ça plutôt simple…

2008/9/1 Pierre V. [email protected]

If you have a layout<
only a single action or a set of actions. The

.>around

the action render call
to use a different
class WeblogController < ActionController::Base
.>instead
of the controller-wide “weblog_standard”
layout<
http://api.rubyonrails.org/classes/ActionController/Layout/ClassMetho…>.


Michel B.


Michel B.

Merci Michel. Donc effectivement aucune solution pour faire simplement
ce que je voulais :slight_smile:

On 1 sep, 07:33, “Michel B.” [email protected]

Oui, je suis d’accord (mais fainéant :slight_smile:
Merci à vous deux pour les astuces.

On 1 sep, 10:28, “Michel B.” [email protected]

je n’ai pas vu le screencast, mais le sujet peut peut-etre se prêter à
ton pb : #125 Dynamic Layouts - RailsCasts

gUI


Pour la santé de votre ordinateur, préférez les logiciels libres.
Lire son mail : http://www.mozilla-europe.org/fr/products/thunderbird/
Browser le web : http://www.mozilla-europe.org/fr/products/firefox/
Suite bureautique : http://fr.openoffice.org/