0

我想覆盖 rails 如何创建视图 *.html.erb

在 ActionView 包中,我已经尝试过这样做。正在做

class ERB
  class Compiler # :nodoc:
..
 class Buffer # :nodoc:
  def compile(s)
   ...
   #It stores in a buffer each ruby chunk in the views inside of a Buffer.
  end
 end
   end
...
 # Here it is where is called compile method. 
 # The thing is that if my view is made up of several *.html.erb files such as partials this method will be invoked each time.        
        #INVOKED PER EACH html.erb file

 def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
     puts ">>> initialize"    
     @safe_level = safe_level
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => "%d %b %Y")
     compiler = ERB::Compiler.new(trim_mode)
    # raise "need a block" 
    # ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:default => nil)
     set_eoutvar(compiler, eoutvar)
     @src = compiler.compile(str)  
     @filename = nil
   end
 end
end

我想知道底部的过程何时开始。我的意思是我必须重写哪个类、文件等,以查看 Rails 从何处开始为特定视图调用所有 html.erb。

我想我应该在这里:

require 'delegate'
require 'optparse'
require 'fileutils'
require 'tempfile'
require 'erb'

module Rails
  module Generator
    module Commands

        class Create 

              # Generate a file for a Rails application using an ERuby template.
              # Looks up and evaluates a template by name and writes the result.
              #
              # The ERB template uses explicit trim mode to best control the
              # proliferation of whitespace in generated code.  <%- trims leading
              # whitespace; -%> trims trailing whitespace including one newline.
              #
              # A hash of template options may be passed as the last argument.
              # The options accepted by the file are accepted as well as :assigns,
              # a hash of variable bindings.  Example:
              #   template 'foo', 'bar', :assigns => { :action => 'view' }
              #
              # Template is implemented in terms of file.  It calls file with a
              # block which takes a file handle and returns its rendered contents.
              def template(relative_source, relative_destination, template_options = {})
                puts "EEEEEEEEEEEEEEEEEEEEEe"
                file(relative_source, relative_destination, template_options) do |file|
                  # Evaluate any assignments in a temporary, throwaway binding.
                  vars = template_options[:assigns] || {}
                  b = template_options[:binding] || binding
                  vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }

                  # Render the source file with the temporary binding.
                  ERB.new(file.read, nil, '-').result(b)
                end
             end

        end

    end
  end
end

但是我没有从 puts 方法中找到任何痕迹。

所有重命名都放在一个名为的文件/lib/*_extensions.erb中,/config/initializers/extensions.rb我有下一个:

Dir[File.dirname(__FILE__) + "/../../lib/*_extensions.rb"].each do |fn|
  require fn
end

我不想透露我为什么这样做。

谢谢

4

1 回答 1

0

我想使用一个 before_render 它至少在 Rails 2.3.4 中不存在。

所以如果你想这样做,我做了下一个:

    class ApplicationController < ActionController::Base
      helper :all # include all helpers, all the time
      protect_from_forgery # See ActionController::RequestForgeryProtection for details

      # Scrub sensitive parameters from your log
      # filter_parameter_logging :password

      protected
      def render(options = nil, extra_options = {}, &block) #:doc:
        puts "BEFORE render"
        puts "Setting %H:%M:%S"
        ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => "%H:%M:%S")
        # call the ActionController::Base render to show the page
        super
       puts "AFTER render"
       puts "Resetting"
       ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:default => nil)
      end

end
于 2010-12-08T10:27:46.573 回答