This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/89

将来的には修正される可能性がありますが、現状のRuby on Railsでは、

render_component :controller => 'entry', :action => 'show', :layout => false
のように、render_componentでlayoutをfalseに指定しても、layoutを無効化する事ができません (「render_component :layout => false doesn't work」参照)

そこで、render_compenentの呼び出し元のコントローラに応じて、レイアウトを自由に設定する方法を紹介します。この方法を使えば、render_componentでlayoutを無効化する事ができるようになります。

やりかたは、render_compenentから呼び出されるアクションの記述を以下のようにするだけです。

EntryController < ApplicationController
  def show
    case parent_controller
    when TopController; render :layout => false
    when RankingController; render :layout => 'ranking'
    end
  end
end
Ruby on Railsでは、render_componentからアクションが呼び出された場合、呼び出し元のコントローラがparent_controllerに設定されます。これを利用して、呼び出しもとのコントローラに応じてレイアウトを自由に変更することができます。

This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/89

posted by Png genki on Sat 26 Aug 2006 at 05:02

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry56
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ