Hostmonster升级到Rails2.3.3

前两天Hostmonster把Rails升级到2.3.3,导致我的网站无法访问。查看日志,Dispatcher failed to catch: undefined method read' for classFCGI::Stream' (NameError),给Hostmonster提交了ticket,到现在都还没有结果,没办法,只能靠自己了。

google了一下,可能是rack中的一段代码的问题。

首先,安装好自己的gem repository,并且在environment.rb中指定:

ENV['GEM_PATH'] = '/home7/huangzhi/ruby/gems:/usr/lib/ruby/gems/1.8'

然后,指定app的rails为2.3.3:

RAILS_GEM_VERSION = '2.3.3' unless defined? RAILS_GEM_VERSION

最后,修改gem中rack-1.0.0/lib/rack/handler/fastcgi.rb文件,将第7行注释掉

#  alias _rack_read_without_buffer read

OK,这样就可以顺利访问啦!

Posted in  hostmonster rails


java读取配置文件

最近在完善contact-list项目,打算把一些配置写配置文件里,就像log4j的做法一样,代码如下:

package com.huangzhimin.contacts.utils;

import java.net.URL;
import java.util.Properties;

public class SystemConfig {

    private static Properties props = null;

    static {
        try {
            props = new Properties();
            URL url = Thread.currentThread().getContextClassLoader().getResource("contactlist.properties");
            if (url != null) {
                props.load(url.openStream());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getProperty(String key) {
        return getProperty(key, "");
    }

    public static String getProperty(String key, String defaultValue) {
        if (props != null && props.getProperty(key) != null) {
            return props.getProperty(key);
        }
        return defaultValue;
    }
}

在类加载的时候,读取classpath中的contactlist.properties文件,然后通过getProperty方法获取配置的值。

Posted in  java


使用active_scaffold做为项目的后台管理

最近做了一个很小的网站,需要一个简单的后台管理来对数据进行审核,由于只是个人用用,不想太浪费时间,就选用active_scaffold插件来做后台管理。对于小项目来说,应用active_scaffold可以快速地构建起项目的后台管理,自己不需要写view,相信大多数程序员都不怎么喜欢吧,controller也只需简单的配置即可。

下面介绍一下我使用active_scaffold作为后台管理的方式吧:

1. 在routes.rb中定义admin namespace

map.namespace :admin do |admin|
  admin.connect 'admin/:controller/:action/:id'
  admin.connect 'admin/:controller/:action/:id.:format'
end

2. 定义AdminController基类,指明其子类必须是admin用户才能操作,以及layout

# app/controller/admin_controller.rb
class AdminController < ApplicationController
  before_filter :login_required, :admin?
  layout 'admin'

  protected
    def admin?
      current_user.admin?
    end
end

3. 后台管理相关的controller都继承AdminController,只需配置active_scaffold即可。

4. 由于对model定义了default_scope :conditions => {:verify => true},导致在后台页面也无法看到没有被验证的数据,只能通过修改active_scaffold源码

--- a/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb
+++ b/vendor/plugins/active_scaffold/lib/active_scaffold/finder.rb
@@ -132,7 +132,7 @@ module ActiveScaffold
     # TODO: this should reside on the model, not the controller
     def find_if_allowed(id, action, klass = nil)
       klass ||= active_scaffold_config.model
-      record = klass.find(id)
+      record = klass.send(:with_exclusive_scope){ klass.find(id) }
       raise ActiveScaffold::RecordNotAllowed unless record.authorized_for?(:action => action.to_sym)
       return record
     end
@@ -162,19 +162,19 @@ module ActiveScaffold
       finder_options.merge! custom_finder_options

       # NOTE: we must use :include in the count query, because some conditions may reference other tables
-      count = klass.count(finder_options.reject{|k,v| [:select, :order].include? k})
+      count = klass.send(:with_exclusive_scope) { klass.count(finder_options.reject{|k,v| [:select, :order].include? k}) }

       finder_options.merge! :include => full_includes

       # we build the paginator differently for method- and sql-based sorting
       if options[:sorting] and options[:sorting].sorts_by_method?
         pager = ::Paginator.new(count, options[:per_page]) do |offset, per_page|
-          sorted_collection = sort_collection_by_column(klass.find(:all, finder_options), *options[:sorting].first)
+          sorted_collection = sort_collection_by_column(klass.send(:with_exclusive_scope){klass.find(:all, finder_options)}, *options[:sorting].first)
           sorted_collection.slice(offset, per_page)
         end
       else
         pager = ::Paginator.new(count, options[:per_page]) do |offset, per_page|
-          klass.find(:all, finder_options.merge(:offset => offset, :limit => per_page))
+          klass.send(:with_exclusive_scope){klass.find(:all, finder_options.merge(:offset => offset, :limit => per_page))}
         end
       end

这样,一个简单的符合我需求的后台管理就搞定了

Posted in  rails rails plugins


ruby正则的named capture

之前用python re的时候,特别喜欢用named capture,主要是可读性好太多了,一个正则表达式写出来都不用再加注释了。

可是ruby1.8并不支持,每次用$1, $2的时候都觉得很ugly,幸好ruby1.9开始支持named capture了。看看example:

result = %r{(?\w+)\s(?\w+)}.match("Richard Huang") result.lastname

=> "Richard"

result.firstname

=> "Huang"通过named capture,别人读你的正则表达式时,也能够轻松地理解你的意图

Posted in  ruby


肌肉痉挛

上上周开始,晚上跑步跑到第三圈,左小腿外侧的肌肉就会绷得很紧,跑起来很疼,只能停下来,一连好几次,每次都是到第三圈开始疼,平时走跳都没感觉,奇了怪 了。老是这样也没办法,只能去医院看看。

昨天趁着放假去了旁边的地段医院看,和医生描述了一遍,医生说应该是肌肉痉挛,问其原因,答曰,讲不清,汗!做了个短波理疗,就是两个圆状的东西放在小腿上,一会小腿就会发热。还蛮舒服的。医生说做一次理疗就可以了,明天晚上再跑跑看,希望不要影响8.8的长跑。

PS:还是第一次拿医保卡去医院看病,分文不用出,不错。

Posted in  life


Fork me on GitHub