2

特定のカスタム HTML タグを探して erb ファイルをスキャンし (私はこの部分を実行しました)、レンダリングする前に、これらのタグをインターセプトし、それらの html 出力を置き換えたいと考えています。RAILS でのこの種の活動に関する情報は見つかりません。多分私は正しい場所を探していません。

4

1 に答える 1

2

多分あなたは次のようにすることができます:

class PostsController < ApplicationController
  acts_as_special

  def show
    @post = Post.find(params[:id])
    respond_to do |format|
      format.html { my_renderer }
    end
  end
end

プラグインまたは sth を記述します。

# Module to Extend a given Controller with the acts_as_special methods

module MyRenderer
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods

    def acts_as_special
      include MyRenderer::InstanceMethods
    end
  end

  module InstanceMethods
    def my_renderer
      .. do sth with the code ....
      render :template => ...
    end
  end
end


ActionController::Base.class_eval do
  include MyRenderer
end

プラグインを作成する必要はありません。コントローラーで「独自の」レンダリング メソッドを使用できるようにする必要があります。

別の/より良い方法がある場合は、お知らせください。

于 2010-08-25T14:55:14.183 に答える