ラベル rails の投稿を表示しています。 すべての投稿を表示
ラベル rails の投稿を表示しています。 すべての投稿を表示

2010/01/04

Rails gem 2.2.2 > 2.3.5 アップグレード時にハマリマス

Rails を 2.3.5にフリーズした後、さっそくエラー
`report_activate_error': RubyGem version error: rack(1.0.0 not ~> 1.0.1) Gem::LoadError)
なるほど、rack が必要そうなので、gem をインストール
gem install rack
Successfully installed rack-1.1.0
1 gem installed
これでも動かない。そりゃ、そうでですね。よく見たら必要なバージョンは 1.0.1。気を取り直して、バージョン指定で。
gem install rack --version 1.0.1 --no-rdoc --no-ri
Successfully installed rack-1.0.1
1 gem installed
これでOK。

特定のバージョンで rails の gem をフリーズする

特定のバージョンでフリーズしたいこともあるなり。
rake VERSION=2.3.5 rails:freeze:gems
config/environments.rb の編集も忘れずに
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION

Rails 言語をパスで管理する方法

http://waraukigoya.com/ja/mypage のように、URLパスの一部を使って言語管理する方法があります。 パッチがあったので適用。
diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb
index ff44849..f3a4f09 100644
--- a/actionpack/lib/action_controller/routing/route_set.rb
+++ b/actionpack/lib/action_controller/routing/route_set.rb
@@ -170,18 +170,24 @@ module ActionController
               def #{selector}(*args)
                 #{generate_optimisation_block(route, kind)}
 
+                locale = #{route.segment_keys.include?(:locale) ? '{ :locale => I18n.locale || I18n.default_locale }' : '{}'}
+
                 opts = if args.empty? || Hash === args.first
                   args.first || {}
                 else
                   options = args.extract_options!
-                  args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)|
+
+                  segments = #{route.segment_keys.inspect}
+                  #{'segments.delete(:locale) if segments.size > args.size' if route.segment_keys.include?(:locale)}
+
+                  args = args.zip(segments).inject({}) do |h, (v, k)|
                     h[k] = v
                     h
                   end
                   options.merge(args)
                 end
 
-                url_for(#{hash_access_method}(opts))
+                url_for(#{hash_access_method}(locale.merge(opts)))
               end
               protected :#{selector}
             end_eval
次に routes.rb ファイルを設定する必要あり。
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
  
  # ====================================================
  # Put all non locale controlled routes above this line
  # 通常ルートの下に、path_prefix で管理したルートを設定
  # ====================================================
  
  map.resources :mypages, :path_prefix => "/:locale"
  map.root :controller => "mypages", :action => "index", :locale => "ja"
  
  map.connect '/:locale/:controller/:action/:id'
  map.connect '/:locale/:controller/:action/:id.:format'
これをやっておけば、リンクヘルパーを使った際、自動的に現在の locale が引き継がれるので便利。
例えば、http://mysite.com/ja/home

の中で、<%= link_to "記事", mypages_path %> と書くと、次のようになる

<a href="http://mysite.com/ja/mypages">記事
もちろん、アプリケーションコントローラーで言語の設定は必要。

Making a link to change locale


application_helper.rb
  def change_locale(locale)
    link = request.parameters
    link[:locale] = locale
    link
  end