Rails2.3.10でrender_componentを使う
Rails2.3.10でrender_componentを使いたい。こちらには下記のように書いてある。
render_component goes from “deprecated” to “nonexistent” in Rails 2.3. If you still need it, you can install the render_component plugin.
install
% script/plugin install git://github.com/rails/render_component.git
サーバを起動するとメソッドがないと言われる
% sudo script/server -p 80 => Booting WEBrick => Rails 2.3.10 application starting on http://0.0.0.0:80 /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/core_ext/module/aliasing.rb:33:in `alias_method': undefined method `set_session_options' for class `ActionController::Base' (NameError) from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/core_ext/module/aliasing.rb:33:in `alias_method_chain' from /home/admin/apps/hoge/vendor/plugins/render_component/lib/components.rb:13:in `included' from /home/admin/apps/hoge/vendor/plugins/render_component/lib/components.rb:3:in `class_eval' from /home/admin/apps/hoge/vendor/plugins/render_component/lib/components.rb:3:in `included' from /home/admin/apps/hoge/vendor/plugins/render_component/init.rb:2:in `include' from /home/admin/apps/hoge/vendor/plugins/render_component/init.rb:2:in `send' from /home/admin/apps/hoge/vendor/plugins/render_component/init.rb:2:in `evaluate_init_rb' from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/rails/plugin.rb:158:in `evaluate_init_rb' ... 16 levels... from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/commands/server.rb:84 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from script/server:3
lackac/render_component が良いという噂
こちらによると、下記のところから持って来るといいよと書いてある。
% git://github.com/lackac/render_component.git
やってみる。さっきinstallした、render_componentは削除する。
% rm -fr vendor/plugins/render_component/ % script/plugin install git://github.com/lackac/render_component.git fatal: remote error: Could not find Repository lackac/render_component
http://github.com/lackac/render_component/tree/master が 404 だし・・・。
lib/components.rb を少しいじれば良いという噂
こちらによると、lib/components.rb の 13行目の :set_session_options を :session= にすれば良いとと書いてある。
# install % script/plugin install git://github.com/rails/render_component.git # 確認 % sed -e "s/:set_session_options/:session=/g" vendor/plugins/render_component/lib/components.rb | diff -u vendor/plugins/render_component/lib/components.rb - # 変更 % sed -i "s/:set_session_options/:session=/g" vendor/plugins/render_component/lib/components.rb # 確認 % head -n 20 vendor/plugins/render_component/lib/components.rb
別のエラーがでた・・・。
% sudo script/server -p 80 パスワード: => Booting WEBrick => Rails 2.3.10 application starting on http://0.0.0.0:80 /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/core_ext/module/aliasing.rb:34:in `alias_method': undefined method `session_with_render_component=' for class `ActionController::Base' (NameError) from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/core_ext/module/aliasing.rb:34:in `alias_method_chain' from /home/admin/apps/hoge/vendor/plugins/render_component/lib/components.rb:13:in `included' from /home/admin/apps/hoge/vendor/plugins/render_component/lib/components.rb:3:in `class_eval' from /home/admin/apps/hoge/vendor/plugins/render_component/lib/components.rb:3:in `included' from /home/admin/apps/hoge/vendor/plugins/render_component/init.rb:2:in `include' from /home/admin/apps/hoge/vendor/plugins/render_component/init.rb:2:in `send' from /home/admin/apps/hoge/vendor/plugins/render_component/init.rb:2:in `evaluate_init_rb' from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/rails/plugin.rb:158:in `evaluate_init_rb' ... 16 levels... from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/commands/server.rb:84 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from script/server:3
lib/components.rb を少しいじれば良いという噂2
こちらがソース。やってみる。さっきinstallした、render_componentは削除する。
% rm -fr vendor/plugins/render_component/ % script/plugin install git://github.com/rails/render_component.git
以下は、vendor/plugins/render_component/lib/components.rb の変更の差分
@@ -10,7 +10,7 @@ attr_accessor :parent_controller alias_method_chain :process_cleanup, :render_component - alias_method_chain :set_session_options, :render_component +# alias_method_chain :set_session_options, :render_component alias_method_chain :flash, :render_component alias_method_chain :assign_shortcuts, :render_component alias_method_chain :send_response, :render_component @@ -134,9 +134,9 @@ end def assign_shortcuts_with_render_component(request, response) - assign_shortcuts_without_flash(request, response) + assign_shortcuts_without_render_component(request, response) flash(:refresh) flash.sweep if @_session && !component_request? end end -end \ ファイル末尾に改行がありません +end
この噂で動いた。
環境
ruby | ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-linux] |
rails | Rails 2.3.10 |
OS | CentOS release 5.3 (Final) |
2011/06/09 追記:「render_component と flash」について
Rails2.3にrender_componentを導入すると、Flashが使えなくなる問題があります。-http://railsforum.com/viewtopic.php?id=34367
下記のように修正すれば、Flashが使えるようになります。
@@ -37,7 +37,7 @@ module InstanceMethods # Extracts the action_name from the request parameters and performs that action. def process_with_components(request, response, method = :perform_action, *arguments) #:nodoc: - flash.discard if component_request? +# flash.discard if component_request? process_without_components(request, response, method, *arguments) end @@ -136,7 +136,7 @@ def assign_shortcuts_with_render_component(request, response) assign_shortcuts_without_render_component(request, response) flash(:refresh) - flash.sweep if @_session && !component_request? +# flash.sweep if @_session && !component_request? end end end
しかし、コンポーネント機能は Rails2.0 で非推奨となり、Rails2.3 でサポートから外れました。render_component は Rails2.3 で plugin 化しました。このように、コンポーネント機能はrailsから離れつつありますので、2.3以降は、render_component自体も使わない方が良いかも知れませんね。