第11å: Bundler
2010/05/17
ååã¾ã§ã§ãã¿ã¹ã¯ç®¡çã¢ããªã±ã¼ã·ã§ã³ Nchak ã¯ããªãå½¢ã«ãªã£ã¦ãã¾ããã
ã¾ã ãã¿ã¹ã¯ã®ååãæ¥ä»ãå¤æ´ããæ©è½ãããã¾ããããä»åã¯å°ã趣åãå¤ãã¦ãRails 3.0 ã®ç®çã§ãã Bundler ã®è©±ããã¾ãããã
Bundler ã¯ãRuby ã¢ããªã±ã¼ã·ã§ã³ãåä½ããã®ã«å¿ è¦ãªãå¥ã®è¨ãæ¹ãããã°ãã¢ããªã±ã¼ã·ã§ã³ãä¾åãã Gem ããã±ã¼ã¸ã®ç®¡çãã¼ã«ã§ãã
Rails 2.x ã§ã¯ãconfig/environment.rb
ã config/environments/*.rb
㧠config.gem
ã¡ã½ããã使ç¨ãã¦ãä¾åãã Gem ããã±ã¼ã¸ã宣è¨ããrake gems:install
ã§ã¾ã¨ãã¦ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãããBundler ã¯ããã«ä»£ããããã±ã¼ã¸ç®¡çæ©æ§ã§ãã
Gemfile
Bundler ã§ã¯ãRails ã¢ããªã±ã¼ã·ã§ã³ã®ã«ã¼ããã£ã¬ã¯ããªã«ãã Gemfile
ã«ãä¾åããããã±ã¼ã¸ãåæãã¾ãã
åæç¶æ ã§ã¯ã次ã®ããã«ãªã£ã¦ããã¯ãã§ãã
source 'http://rubygems.org' gem 'rails', '3.0.0' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' gem 'sqlite3-ruby', :require => 'sqlite3' # Use unicorn as the web server # gem 'unicorn' # Deploy with Capistrano # gem 'capistrano' # Bundle the extra gems: # gem 'bj' # gem 'nokogiri' # gem 'sqlite3-ruby', :require => 'sqlite3' # gem 'aws-s3', :require => 'aws/s3' # Bundle gems for the local environment. Make sure to # put test-only gems in this group so their generators # and rake tasks are available in development mode: # group :test do # gem 'webrat' # end
ããã次ã®ããã«ä¿®æ£ãã¦ãã ããã
source 'http://rubygems.org' gem 'rails', '3.0.0' gem 'sqlite3-ruby', :require => 'sqlite3' gem 'will_paginate', '3.0.pre2'
ã³ã¡ã³ãè¡ãé¤ãã¨ãå ã æ¸ããã¦ããããã±ã¼ã¸ã¯ rails 㨠sqlite-ruby ã ãã§ããããã« will_paginate ã追å ãã¦ãã¾ãã
å·çæç¹(2010/05/20)ã§ã¯ãwill_paginate ã®ææ°ç㯠2.3.12 ã§ããããã㯠Rails 3.0 ã«å¯¾å¿ãã¦ãã¾ãããããã§ã¯ãβçã®ããã±ã¼ã¸ã使ãããã'3.0.pre' ã¨ãã¼ã¸ã§ã³çªå·ãæå®ãã¦ãã¾ãã
[æ´æ°] will_paginateã®ãã¼ã¸ã§ã³ã 3.0.pre ãã 3.0.pre2 ã«å¤æ´ãã¾ããã3.0.pre ã§ã¯ãDEPRECATION WARNING: railtie_name is deprecated and has no effect.ãã¨ããè¦åãåºããã¨ããèªè ã®æ¹ããæãã¦ããã ãã¾ããã(2011/5/9)
ã¤ã³ã¹ãã¼ã«
Bundler ã¯ãã³ãã³ãä¸çºã§ä¾åé¢ä¿ã調ã¹ãå¿ è¦ãªãã¹ã¦ã®ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã
ã¾ããå¿ è¦ãªããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããã¦ãããã©ãã調ã¹ã¾ãã
% bundle check Your Gemfile's dependencies could not be satisfied Install missing gems with `bundle install`
ç¶ãã¦ãã¤ã³ã¹ãã¼ã«ãã¾ãã
% bundle install Fetching source index from http://rubygems.org/ Using rake (0.8.7) from bundler gems Using abstract (1.0.0) from system gems Using builder (2.1.2) from system gems Using i18n (0.3.7) from system gems Using memcache-client (1.8.3) from bundler gems Using tzinfo (0.3.20) from bundler gems Using activesupport (3.0.0) from bundler gems Using activemodel (3.0.0) from bundler gems Using erubis (2.6.5) from system gems Using rack (1.1.0) from system gems Using rack-mount (0.6.3) from bundler gems Using rack-test (0.5.3) from system gems Using actionpack (3.0.0) from bundler gems Using mime-types (1.16) from system gems Using polyglot (0.3.1) from system gems Using treetop (1.4.5) from system gems Using mail (2.2.1) from bundler gems Using text-hyphen (1.0.0) from system gems Using text-format (1.0.0) from system gems Using actionmailer (3.0.0) from bundler gems Using arel (0.3.3) from system gems Using activerecord (3.0.0) from bundler gems Using activeresource (3.0.0) from bundler gems Using bundler (0.9.25) from system gems Using cgi_multipart_eof_fix (2.5.0) from system gems Using daemons (1.0.10) from system gems Using fastthread (1.0.7) from system gems Using gem_plugin (0.2.3) from system gems Using thor (0.13.6) from system gems Using railties (3.0.0) from bundler gems Using rails (3.0.0) from bundler gems Using sqlite3-ruby (1.2.5) from system gems Installing will_paginate (3.0.pre2) from rubygems repository at http://rubygems.org/ Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
Linux ããã³ Mac OS X ã¦ã¼ã¶ã¼ã¸ã®æ³¨æãbundle install ã®åã« sudo ãä»ããããªãããããã¾ããããä»ããªãã§ãã ãããbundle ã³ãã³ãã¯ãå®è¡éä¸ã§ããªãã®ãã¹ã¯ã¼ããå°ããroot 権é㧠Gem ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã(å çè¨æ£ã2010-10-11)
ãµã¼ãã¼ãåãã¦ããããããã§åèµ·åãã¦ãã ãããåèµ·åãå¿ãã㨠undefined method paginate for Class
ã¨ããã¨ã©ã¼ãåºã¾ãã
èªè ã®æ¹ããã®ææã«ããããµã¼ãã¼åèµ·åã®ä»¶ã追å ãã¾ããã(2011-06-15)
ãã¼ã¸ãã¼ã·ã§ã³
will_pagination ããã±ã¼ã¸ãå©ç¨ãã¦ãã¿ã¹ã¯ãªã¹ãã«ãã¼ã¸ãã¼ã·ã§ã³æ©è½ã追å ãã¾ãããã
ã¾ããã·ã¼ããã¼ã¿ãç¨æãã¾ããdb/seeds/development/tasks.rb
ãéãã¦ã次ã®ããã«ä¿®æ£ãã¦ãã ããã
Task.create(:name => "Task 0", :due_date => Date.today, :done => true) 1.upto(100) do |n| Task.create(:name => "Task #{n}", :due_date => n.days.from_now, :done => false) end
ã·ã¼ããã¼ã¿ãæå ¥ãã¾ãã
% rake db:reset
app/controllers/tasks_controller.rb
ãéãã¦ãã¢ã¯ã·ã§ã³ index 㨠done ãä¿®æ£ãã¾ãã
class TasksController < ApplicationController def index @task = Task.new @tasks = Task.undone.paginate(:page => params[:page], :per_page => 10) end def done @task = Task.new @tasks = Task.done.paginate(:page => params[:page], :per_page => 10) render :action => 'index' end (çç¥) end
app/views/tasks/index.html.erb
ãä¿®æ£ãã¾ãã
(çç¥) </table> <%= will_paginate(@tasks) %>
æ°è¦ãã¡ã¤ã« public/stylesheets/pagination.css
ã次ã®ããã«ä½æãã¾ãã
div.pagination { text-align: center; padding: 5px; } div.pagination a { background-color: #666; color: #fff; padding: 5px; } div.pagination em { background-color: #ccc; font-weight: bold; font-style: normal; padding: 5px; } div.pagination span.disabled { background-color: #ccc; padding: 5px; }
åä½ç¢ºèª
ãã©ã¦ã¶ã§ã¿ã¹ã¯ä¸è¦§ãã¼ã¸ãéãã¾ãã
ãNextããªã³ã¯ãã¯ãªãã¯ããã¨ã2ãã¼ã¸ç®ã表示ããã¾ãã