20th Tue
RailsでレイアウトをNestさせる方法
This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/174
Railsのレイアウトの仕組みは、レイアウトを明示的に指定しない場合は、fooコントローラに対してapp/views/layouts/foo.rhmltが存在する場合はfoo.rhtmlをレイアウトと見なし、存在しない場合はapp/views/layouts/application.rhtmlをレイアウトとして使用するようになっています。 両方とも存在しない場合はレイアウト無しと見なされます。
これを、foo.rhtmlをレイアウトとして使用しつつ、さらにその外側のレイアウトとしてapplication.rhtmlを使用するようにするには、コントローラに以下のようなafter_filter を追加すればOKです。
class FooController < ApplicationController
after_filter :wrapping_output_with_application_layout
private
def wrapping_output_with_application_layout
return true if @performed_redirect
content_for_layout = response.body
erase_render_results
add_variables_to_assigns
@template.instance_variable_set("@content_for_layout", content_for_layout)
render_text(@template.render_file('layouts/application', true))
end
end
コントローラ毎のレイアウトの共通部分をさらに括り出したい場合に便利です。
This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/174
posted by
genki on Tue 20 Mar 2007 at 16:56 with 0 comments