class SampleController < ApplicationController
#filterを利用するのがポイントです
before_filter :update_table_config
active_scaffold :samples do |config|
#ここの処理はだいぶ早い段階で処理されるようでsessionオブジェクトが生成されていません
config.label = "Sample"
・・・
end
protected
def update_table_config
#ここでsessionを利用して変更ができます。
#Uesrモデルをsession[:user]にセットする処理はApplicationControllerなどで行います
#年齢によって変更できるカラムが異なる例です
if session[:user].age >= 20
active_scaffold_config.update.columns = [:hobby,:smoke,:sake]
else
active_scaffold_config.update.columns = [:hobby]
end
end
end
active_scaffoldブロックの中で記載している設定をfilterではactive_scaffold_configというのに記載すればsession情報によって画面内容を変更できます。
ただfilter内ではすべての設定項目を変更できるわけではなさそうです。
試した限りactionsとnestedは利用できませんでした。
以下が参考になります。
https://github.com/activescaffold/active_scaffold/wiki/Per-Request-Configuration
ちなみに、このブログでactive_scaffoldに関しての情報を他にも乗せています。
ActiveScaffoldの小ネタのまとめ
0 件のコメント:
コメントを投稿