ZoOL => Blog

I'm ZoOL!

去除invaild Gemspec警告

在某些版本gems里,由于spac日期解析的问题, 会有一些Invaild gemspec的警告

1
Invalid gemspec in [/Users/ZoOL/.rvm/gems/ruby-1.9.3-p0/specifications/oauth2-0.5.2.gemspec]: invalid date format in specification: "2012-01-04 00:00:00.000000000Z"

用手工去除gemspec文件, 比较麻烦 另一种是直接用代码实现

1
perl -p -i -e 's/ 00:00:00.000000000Z//' ~/.rvm/gems/*/specifications/*.gemspec

在路由中添加参数

由于业务需要, 要在两个url地址中完成类似的操作, 于是想到了在路由配置中设置参数来解决需求

1
2
  match '/home/action1' => 'home#action1'
  match '/home/action2' => 'home#action1', :has_params => 'yes'

这样就在action2的路由里添加了参数 params[:has_params], 其值为 ‘yes’

设置rails中generator使用的orm类型

在最近的一个项目里, 同时使用了postgresql和mongoid,

结果在rails g model xxxx时, 默认调用的是mongoid,

虽然能通过–orm=actvie_record参数指定orm, 但还是很麻烦.

通过查询手册, 可以用下面的设置方式解决.

1
2
3
4
#config/application.rb
config.generators do |g|
   g.orm             :active_record
end

增加carrierwave对rails_admin的支持

由于rails_admin默认只是支持paperclip, 所以使用carrierwave的话, 需要手动做些修改.

一共3个文件

1
2
3
4
5
6
7
8
9
10
11
12
#app/views/rails_admin/main/_form_carrier_wave_file.html.haml
= label_tag "#{field.abstract_model.to_param}_#{field.name}", field.label
.input
  - if field.bindings[:object].send("#{field.name}_url")
    .row
      = link_to field.bindings[:object].send("#{field.name}_url")
      %br
      = form.check_box "remove_#{field.name}"
      = form.label "remove_#{field.name}", "Remove existing #{field.label.downcase}", :class => "inline"
  .row
    = form.file_field field.name, :class => "fileUploadField #{field.has_errors? ? "errorField" : nil}"
    = form.hidden_field "#{field.name}_cache"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#app/views/rails_admin/main/_form_carrier_wave_image.html.haml

= label_tag "#{field.abstract_model.to_param}_#{field.name}", field.label
.input
  - image = field.bindings[:object].send(field.name)
  - if image.path # the most direct check of an assets existence I could see
    .row
      -# load a default 'version' if it exists. should really be set through rails_admin's DSL:
      - default_version = image.versions[:main]
      = image_tag default_version && default_version.url || image.url
      %br
      = form.check_box "remove_#{field.name}"
      = form.label "remove_#{field.name}", "Remove existing #{field.label.downcase}", :class => "inline"
  .row
    = form.file_field field.name, :class => "fileUploadField #{field.has_errors? ? "errorField" : nil}"
    = form.hidden_field "#{field.name}_cache"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#config/initializers/rails_admin.rb

# Register a custom field factory and field type for CarrierWave if its defined
if defined?(::CarrierWave)
  module RailsAdmin::Config::Fields::Types
    # Field type that supports CarrierWave file uploads
    class CarrierWaveFile < RailsAdmin::Config::Fields::Types::FileUpload
      register_instance_option(:partial) do
        :form_carrier_wave_file
      end
    end

    # Field type that supports CarrierWave file uploads with image preview
    class CarrierWaveImage < CarrierWaveFile
      register_instance_option(:partial) do
        :form_carrier_wave_image
      end
    end

    # Register field type to the types registry
    register(:carrier_wave_file, CarrierWaveFile)
    register(:carrier_wave_image, CarrierWaveImage)
  end
  RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
    model = parent.abstract_model.model
    if model.kind_of?(CarrierWave::Mount) && model.uploaders.include?(properties[:name])
      type = properties[:name] =~ /image|picture|thumb/ ? :carrier_wave_image : :carrier_wave_file
      fields << RailsAdmin::Config::Fields::Types.load(type).new(parent, properties[:name], properties)
      true
    else
      false
    end
  end
end

Rails调试插件rails-footnotes使用说明

说明

rails-footnotes 是一个在development环境下调试rails用的插件, 可以在当前页面的页脚处显示当前的调试信息,如session、实例变量、数据库查询时间等等

安装方式

1
2
3
4
5
6
7
8
9
10
11
#编辑Gemfile, rails3版本必须用rails-footnotes 3.7 版以上的版本
gem 'rails-footnotes', '>= 3.7', :group => 'development'

bundle install

#编辑config/initializers/footnotes.rb
if defined?(Footnotes) && Rails.env.development?
    Footnotes.run! # first of all

    # ... other init code
end

注意

rails-footnotes默认使用textmate来编辑当前页面文件, 如果要自定义配置编辑器, 请看rails-footnotes文档

Rails3rc1在安装rspec-rails时碰到报错

今天在rails3rc1下安装rspec-rails, 提示了以下错误

1
Invalid gemspec in [/Users/ZoOL/.rvm/gems/ruby-1.8.7-p334/specifications/rspec-core-2.6.2.gemspec]: invalid date format in specification: "2011-05-21 00:00:00.000000000Z"

解决方法

找到specifications/rspec-core-2.6.2.gemspec, 将下面这行注释

1
s.date = %q{2011-05-21 00:00:00.000000000Z}

另外在执行rake命令时, 遇到了下面的报错信息

1
undefined method `prerequisites' for nil:NilClass

google后发现是rspec-rails版本的问题, 使用2.6.1.beta1版本以上就好了, 编辑Gemfile

1
  gem 'rspec-rails', '~> 2.6.1.beta'

设置link标签使浏览器自动发现rss标签

1
<link rel="alternate" type="application/atom" title="ZoOL =&gt; Blog" href="http://zool.me/atom.xml" />

上述语句使得浏览器在打开我的博客时,会自动获取blog的rss地址。

另一个rss+xml格式

1
<link rel="alternate" type="application/rss+xml" title="ZoOL =&gt; Blog" href="http://blog.zool.it/feed.rss" /

修复安装gems时Errno::ETIMEDOUT: Operation Timed Out - Connect(2)的错误

这两天更新gems出现下面的错误

1
2
3
4
  ~  gem install rails
ERROR:  Could not find a valid gem 'rails' (>= 0) in any repository
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    Errno::ETIMEDOUT: Connection timed out - connect(2) (http://rubygems.org/latest_specs.4.8.gz)

经检查和GFW无关,是rubygems的DNS 调整问题

问题解决的最好方法方法

使用google的DNS 8.8.8.8 / 8.8.4.4

另一种解决方式

修改rubygems的source源

1
2
3
4
5
6
#删除原有gem source
gem source -r http://rubygems.org/
gem source -r http://production.s3.rubygems.org/

#增加新source源
gem source -a  http://production.s3.rubygems.org.s3.amazonaws.com/

Rails路由设置网站首页

做一个应用必做的一步

首先删除 /public/index.html 这个文件

然后编辑 config/route.rb ,设置路由

1
root :to => "home#index"

其中 “home#index” 是rails路由的简写, 指的是 homescontroller的index方法

设置首页后, 会自动生成两个helper方法 root_path和 root_url, 其中path是相对路径,url是绝对路径