「Ruby on Rails」カテゴリーアーカイブ

Rails2.1でタイムゾーンを扱う

mad.ly - Rails 2.1 Time Zone Support: An Overview --- Rails2.1のタイムゾーンサポートを詳しく解説している。サンプルのアプリもあり分かりやすい。後述する内容も書いてある。
Ruby On Rails ピチカート街道 - Rails 2.1・その12(DBに登録してあるUTC日付データを簡単変換) - --- コントローラで、以下のようにしてタイムゾーンを設定し直せる。

Time.zone = 'Tokyo'

Class: ActiveSupport::TimeZoneクラスを使用すると、Rails2.1のタイムゾーンサポートで設定したタイムゾーンを考慮したTimeオブジェクトの作成やUTCへの変換が簡単にできる。

# 設定したタイムゾーンでローカルのTimeを作成
t = Time.zone.local(2008, 9, 1, 0, 0)
#=> Mon, 01 Sep 2008 00:00:00 JST +09:00
# UTCに変換
utc = Time.zone.local_to_utc(t)
#=> Sun Aug 31 15:00:00 UTC 2008

Railsでmemcachedを使う

mac osxにmemcachedをインストール。

$ sudo port install memcached
Password:
--->  Fetching libevent
--->  Attempting to fetch libevent-1.4.3-stable.tar.gz from http://monkey.org/~provos/
--->  Verifying checksum(s) for libevent
--->  Extracting libevent
--->  Configuring libevent
--->  Building libevent with target all
--->  Staging libevent into destroot
--->  Installing libevent 1.4.3_0
--->  Activating libevent 1.4.3_0
--->  Cleaning libevent
--->  Fetching memcached
--->  Attempting to fetch memcached-1.2.5.tar.gz from http://www.danga.com/memcached/dist/
--->  Verifying checksum(s) for memcached
--->  Extracting memcached
--->  Configuring memcached
--->  Building memcached with target all
--->  Staging memcached into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting memcached with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.memcached.plist
###########################################################
--->  Installing memcached 1.2.5_2
--->  Activating memcached 1.2.5_2
--->  Cleaning memcached

memcachedの自動起動を設定。

$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.memcached.plist

cached_modelをインストール。(memcache-clientもインストールされる。)

$ sudo gem install cached_model

config/environment.rb

require 'memcache'
memcache_options = {
  :c_threshold => 10_000,
  :compression => true,
  :debug => false,
  :namespace => ":app-#{RAILS_ENV}",
  :readonly => false,
  :urlencode => false
}
CACHE = MemCache.new memcache_options
CACHE.servers = 'localhost:11211'
ActionController::Base.session_options[:expires] = 1800
ActionController::Base.session_options[:cache] = CACHE

参考:
memcached Basics for Rails | Ruby on Rails for Newbies
memcached: a distributed memory object caching system
memcached公式サイト
Installing memcached | Development, Analysis And Research
memcahcedのインストール方法
Rails fragment cache with memcached-client and time-based :expire option - skwpspace
railsのfragment cacheでmemcachedを使うためのプラグイン
railsのセッション管理でmemcachedを利用 - dreammindの日記
FFTT : memcached
[memcached] memcached導入手順 - Life with IT

admin/usersコントローラを作成しようとしたらエラー

restful_authenticationを使っているアプリケーションで、AdminモジュールのUsersコントローラをgeneratorで作成しようとしたら、以下のようにエラーになった。

$ ./script/generate controller admin/users
  The name 'Admin::UsersHelper' is either already used in your application or reserved by Ruby on Rails.
  Please choose an alternative and run this generator again.

'Admin::UsersHelper'はないので、原因が分からない。
restful_authenticationを使っていないアプリケーションで同じことをしたら、大丈夫だった。
次に'Admin::Test'コントローラを作成したら、大丈夫だった。

 $ ./script/generate controller admin/test
      create  app/controllers/admin
      create  app/helpers/admin
      create  app/views/admin/test
      create  test/functional/admin
      create  app/controllers/admin/test_controller.rb
      create  test/functional/admin/test_controller_test.rb
      create  app/helpers/admin/test_helper.rb

'Admin::Test'コントローラを作成してから、'Admin::Users'を作成したら、なぜか大丈夫だった。

$ ./script/generate controller admin/users
      exists  app/controllers/admin
      exists  app/helpers/admin
      create  app/views/admin/users
      exists  test/functional/admin
      create  app/controllers/admin/users_controller.rb
      create  test/functional/admin/users_controller_test.rb
      create  app/helpers/admin/users_helper.rb

'Admin::Test'はいらないので削除。

$ ./script/destroy controller admin/test
          rm  app/helpers/admin/test_helper.rb
          rm  test/functional/admin/test_controller_test.rb
          rm  app/controllers/admin/test_controller.rb
    notempty  test/functional/admin
    notempty  test/functional
    notempty  test
       rmdir  app/views/admin/test
    notempty  app/views/admin
    notempty  app/views
    notempty  app
    notempty  app/helpers/admin
    notempty  app/helpers
    notempty  app
    notempty  app/controllers/admin
    notempty  app/controllers
    notempty  app

よく分からないけど、とりあえずこれでOKだったので、メモしておく。

config.gemについて

[Edge Rails] Gemの管理を行う: Gem Dependencies | poqu.log
Ruby On Rails ピチカート街道 - Rails 2.1・その10(gemの依存関係を明示的に指定) -
config.gemで何ができるかについて。
パッケージ名とライブラリ名が異なっている場合は:libなどで正しく指定する必要がある。

config.gem "rmagick", :lib => "RMagick"

Yano lablog - RMagickのインストール for Ubuntu and Rails

rails2.1.0でgettextのエラー

$ ./script/server -b 127.0.0.1
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails 2.1.0 application starting on http://127.0.0.1:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at 127.0.0.1:3000
** Starting Rails with development environment...
Exiting
/Users/pistolfly/railsprojects/test/app/controllers/application.rb:7: undefined method `init_gettext' for ApplicationController:Class (NoMethodError)
	from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:215:in `load_without_new_constant_marking'
	from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:215:in `load_file'
	from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in `new_constants_in'
	from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:214:in `load_file'
	from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:95:in `require_or_load'
	from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:60:in `depend_on'
	from /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:456:in `require_dependency'
	from /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:18:in `define_dispatcher_callbacks'
	 ... 40 levels...
	from /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/server.rb:39
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
	from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `require'
	from ./script/server:3

とりあえずエラーが発生しているApplicationController(application.rb)のinit_gettextをコメントアウトしてmongrelを起動して、ページにアクセスすると、今度は以下のエラー。

  Status: 500 Internal Server Error
  undefined method `file_exists?' for #
    /Library/Ruby/Gems/1.8/gems/gettext-1.91.0/lib/gettext/rails.rb:281:in `render_file'
    /Library/Ruby/Gems/1.8/gems/gettext-1.91.0/lib/gettext/rails.rb:279:in `each'
    /Library/Ruby/Gems/1.8/gems/gettext-1.91.0/lib/gettext/rails.rb:279:in `render_file'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/rescue.rb:181:in `rescue_action_locally'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/rescue.rb:125:in `rescue_action'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/rescue.rb:203:in `perform_action_without_caching'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:13:in `perform_action'
    /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/connection_adapters/abstract/query_cache.rb:33:in `cache'
    /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/query_cache.rb:8:in `cache'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/caching/sql_cache.rb:12:in `perform_action'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:in `send'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:529:in `process_without_filters'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/filters.rb:569:in `process_without_session_management_support'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/session_management.rb:130:in `process'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/base.rb:389:in `process'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:149:in `handle_request'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:107:in `dispatch'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:104:in `synchronize'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:104:in `dispatch'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:120:in `dispatch_cgi'
    /Library/Ruby/Gems/1.8/gems/actionpack-2.1.0/lib/action_controller/dispatcher.rb:35:in `dispatch'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:76:in `process'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `synchronize'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/../lib/mongrel/rails.rb:74:in `process'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:282:in `run'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `each'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/configurator.rb:281:in `run'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:128:in `run'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/lib/mongrel/command.rb:212:in `run'
    /Library/Ruby/Gems/1.8/gems/mongrel-1.1.5/bin/mongrel_rails:281
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in `new_constants_in'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:502:in `load'
    /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/servers/mongrel.rb:64
    /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
    /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `require'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in `require'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:354:in `new_constants_in'
    /Library/Ruby/Gems/1.8/gems/activesupport-2.1.0/lib/active_support/dependencies.rb:509:in `require'
    /Library/Ruby/Gems/1.8/gems/rails-2.1.0/lib/commands/server.rb:39
    /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
    /Library/Ruby/Site/1.8/rubygems/custom_require.rb:27:in `require'
    ./script/server:3

対処方法は以下のページに。
rails2.0.2 → 2.1.0 に移行 - 夜の Discovery
Edge Rails and gettext: undefined method file_exists? (NoMethodError)
config/initializers/gettext.rbを作成する。
内容は以下。

require 'gettext/rails'
module ActionView
  class Base
    delegate :file_exists?, :to => :finder unless respond_to?(:file_exists?)
  end
end

config/initializers/gettext.rbにgettext/railsのrequireを書いたので、config/environment.rbから、gettext/railsの読み込みを削除。

# config/initializers/gettext.rbに移動
#require 'gettext/rails'

この問題は、Rails-2.0.xからRails-2.1.0でActionView::Base#file_exists? メソッドが ActionView::TemplateFinder#file_exists?に移ったことが原因らしい。
Ruby-GetText-1.92.0で対応済みなので、上記回避方法は不要となった。
Rails2.1以降では、environment.rbで、require 'gettext/rails'ではなく、config.gemを使う。

require 'gettext/rails'

ではなく、

Rails::Initializer.run do |config|
  :
  :
  config.gem "gettext", :lib => "gettext/rails"
end