本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7593663
建立时间:2006年5月29日




[Ruby on Rails]Rails代码片段
软件技术

lhwork 发表于 2007/1/19 15:57:25

1Debugging Views in Development ruby 代码 <!--javascript_include_tag 'prototype'-->      <!--/span><span class="keyword">if</span><span> ENV['RAILS_ENV'] == 'development'-->       "debug" style="margin: 40px 5px 5px 5px;">            "#" onclick="Element.toggle('debug_info');return false" style="text-decoration: none; color: #ccc;">Show Debug Info ➲            "debug_info" style="display : none;">                <!--debug session-->                <!--debug params-->             还有一个相关的插件:TextmateFootnotesPlugin ruby 代码 class << Dispatcher      def dispatch(cgi = CGI.new,                   session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS)        begin         request, response =            ActionController::CgiRequest.new(cgi, session_options),            ActionController::CgiResponse.new(cgi)          prepare_application          ActionController::Routing::Routes.recognize!(request).process(request, response).out        rescue Object => exception          begin           ActionController::Base.process_with_exception(request, response, exception).out          rescue           # The rescue action above failed also, probably for the same reason            # the original action failed.  Do something simple which is unlikely            # to fail.  You might want to redirect to a static page instead of this.            e = exception            cgi.header("type" => "text/html")            cgi.out('cookie' => '') do             <<-RESPONSE                                                         Application Error                 #{e.class}: #{e.message}                    #{e.backtrace.join("\n")}                         RESPONSE            end         end       ensure         reset_application        end     end   end     3 SQL Logging in Rails ruby 代码 SQL_LOGGING = true   SQL_LOG_MAX_LINES = 5000    SQL_LOG_FILE = File::join(RAILS_ROOT, '/log/sql_log.txt')    # $sql_log is a global var that will hold the results of the last sql statement executed    $sql_log = ""   # permit logging if we are in development environment    if RAILS_ENV_DEV || RAILS_ENV_TEST      connection = ActiveRecord::Base.connection      class << connection        alias :original_exec :execute       def execute(sql, *name)          # try to log sql command but ignore any errors that occur in this block          # we log before executing, in case the execution raises an error          begin           if SQL_LOGGING              if File::exists?(SQL_LOG_FILE) : lines = IO::readlines(SQL_LOG_FILE)              else lines = Array.new(); end             log = File.new(SQL_LOG_FILE, "w+")              # keep the log to specified max lines              if lines.length > SQL_LOG_MAX_LINES                lines.slice!(0..(lines.length-SQL_LOG_MAX_LINES))              end             lines << Time.now.strftime("%x %I:%M:%S %p")+": "+sql+"n"             log.write(lines)              log.close              $sql_log = sql            end # if          rescue Exception => e            ;          end         # execute original statement          original_exec(sql, *name)        end # def execute      end # class <<    end # if RAILS_ENV_DEV       Here's the constant setting code (put in application.rb or something it requires before the code above gets run):          class CoreERR_RailsEnvironment < StandardError; end   #RAILS specific constants      #setup global constants for manipulating states      RAILS_ENV_VAR = "RAILS_ENV"     #set ENV to development if explicit or not present      RAILS_ENV_DEV = (ENV[RAILS_ENV_VAR]=="development" || (not ENV[RAILS_ENV_VAR]))      RAILS_ENV_LIVETEST = ENV[RAILS_ENV_VAR]=="livetest"     RAILS_ENV_TEST = ENV[RAILS_ENV_VAR]=="test"     if (RAILS_ENV_DEV) or (RAILS_ENV_TEST)        RAILS_ENV_PRODUCTION = false     else       RAILS_ENV_PRODUCTION = true     end     # check positively: if production environment is implied because test and development are not found      # but production doesn't show up positively, raise exception      if RAILS_ENV_PRODUCTION and (RAILS_ENV_PRODUCTION != (ENV[RAILS_ENV_VAR]=="production"))        raise CoreERR_RailsEnvironment, "Production environment implied but not detected: "+ENV[RAILS_ENV_VAR]      end     RAILS_DB_SU = ENV[RAILS_ENV_VAR]+'_su'      RAILS_ENV_DEV.freeze      RAILS_ENV_TEST.freeze      RAILS_ENV_PRODUCTION.freeze      3 Digest MD5 & SHA1 Digest 支援 MD5 和 SHA1 兩種編碼, 你若有儲存密碼的需求就要用到, 一般是用 SHA1. MD5 計算 require 'digest/md5' puts Digest::MD5.hexdigest("Hello World!") 計算檔案的 MD5, 可以確保檔案未曾被修改 require 'digest/md5' #method 1 puts Digest::MD5.hexdigest(File.read("o.rb")) #method 2 class Digest::MD5 def self.open(path) o = new File.open(path) { |f| buf = "" while f.read(256, buf) o << buf end } o end end puts Digest::MD5.open("o.rb").hexdigest SHA1 計算 require 'digest/sha1' puts Digest::SHA1.hexdigest("Hello World!") 最后更新:2007-01-11 20:23


阅读全文(2301) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144760480 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号