【问题标题】:Override rails render method and replace content覆盖 rails 渲染方法并替换内容
【发布时间】:2011-01-03 05:02:25
【问题描述】:

我想扫描我的 erb 文件以查找特定的自定义 HTML 标签(我已经完成了这部分),然后在渲染之前,拦截这些标签并替换它们的 html 输出。我在 RAILS 中找不到与此类活动相关的任何信息。也许我没有找对地方。

【问题讨论】:

    标签: ruby-on-rails rendering-engine


    【解决方案1】:

    也许你可以这样做:

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

    并写一个插件或某事:

    # 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
    

    你不需要编写插件,你必须让“你的”渲染方法可用于控制器。

    如果您有不同/更好的方法,请告诉我!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-11
      • 2016-07-23
      • 1970-01-01
      • 2023-01-12
      • 2011-08-18
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多