私の歴史と今

振り返ると恥ずかしくなるのが私の歴史。だけどそのときは真面目に書いていた訳でね。そんな今の私を書いていく。

Rails4プロジェクトの作成

プロジェクトディレクトリの作成

kenmochi-no-MacBook-Pro:repos junya$ mkdir imacros-skill
kenmochi-no-MacBook-Pro:repos junya$ cd imacros-skill/

使用するRubyバージョンの設定

kenmochi-no-MacBook-Pro:imacros-skill junya$ ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
kenmochi-no-MacBook-Pro:imacros-skill junya$ rbenv versions
  system
* 1.9.3-p392 (set by /Users/junya/.rbenv/version)
  2.0.0-p247
kenmochi-no-MacBook-Pro:imacros-skill junya$ rbenv local 2.0.0-p247
kenmochi-no-MacBook-Pro:imacros-skill junya$ rbenv versions
  system
  1.9.3-p392
* 2.0.0-p247 (set by /Users/junya/Dropbox/repos/imacros-skill/.ruby-version)
kenmochi-no-MacBook-Pro:imacros-skill junya$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
kenmochi-no-MacBook-Pro:imacros-skill junya$ cd ..
kenmochi-no-MacBook-Pro:repos junya$ ruby -v
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
kenmochi-no-MacBook-Pro:repos junya$ cd imacros-skill
kenmochi-no-MacBook-Pro:imacros-skill junya$ ls -la
total 8
drwxr-xr-x   3 junya  staff  102  8  1 00:54 .
drwxr-xr-x  11 junya  staff  374  8  1 00:53 ..
-rw-r--r--   1 junya  staff   11  8  1 00:54 .ruby-version
kenmochi-no-MacBook-Pro:imacros-skill junya$ cat .ruby-version 
2.0.0-p247
kenmochi-no-MacBook-Pro:imacros-skill junya$ 

bundler

kenmochi-no-MacBook-Pro:imacros-skill junya$ gem install bundler
Fetching: bundler-1.3.5.gem (100%)
Successfully installed bundler-1.3.5
Parsing documentation for bundler-1.3.5
Installing ri documentation for bundler-1.3.5
1 gem installed

Gemfile

kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle init
Writing new Gemfile to /Users/junya/Dropbox/repos/imacros-skill/Gemfile
kenmochi-no-MacBook-Pro:imacros-skill junya$ cat Gemfile 
# A sample Gemfile
source "https://rubygems.org"

# gem "rails"
kenmochi-no-MacBook-Pro:imacros-skill junya$ vi Gemfile 

rails4

kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Installing rake (10.1.0) 
Installing i18n (0.6.4) 
Installing minitest (4.7.5) 
Installing multi_json (1.7.7) 
Installing atomic (1.1.10) 
Installing thread_safe (0.1.2) 
Installing tzinfo (0.3.37) 
Installing activesupport (4.0.0) 
Installing builder (3.1.4) 
Installing erubis (2.7.0) 
Installing rack (1.5.2) 
Installing rack-test (0.6.2) 
Installing actionpack (4.0.0) 
Installing mime-types (1.23) 
Installing polyglot (0.3.3) 
Installing treetop (1.4.14) 
Installing mail (2.5.4) 
Installing actionmailer (4.0.0) 
Installing activemodel (4.0.0) 
Installing activerecord-deprecated_finders (1.0.3) 
Installing arel (4.0.0) 
Installing activerecord (4.0.0) 
Using bundler (1.3.5) 
Installing hike (1.2.3) 
Installing thor (0.18.1) 
Installing railties (4.0.0) 
Installing tilt (1.4.1) 
Installing sprockets (2.10.0) 
Installing sprockets-rails (2.0.0) 
Installing rails (4.0.0) 
Your bundle is complete!
It was installed into ./vendor/bundle

help

kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle exec rails new -h
Usage:
  rails new APP_PATH [options]

Options:
  -r, [--ruby=PATH]              # Path to the Ruby binary of your choice
                                 # Default: /Users/junya/.rbenv/versions/2.0.0-p247/bin/ruby
  -m, [--template=TEMPLATE]      # Path to some application template (can be a filesystem path or URL)
      [--skip-gemfile]           # Don't create a Gemfile
  -B, [--skip-bundle]            # Don't run bundle install
  -G, [--skip-git]               # Skip .gitignore file
      [--skip-keeps]             # Skip source control .keep files
  -O, [--skip-active-record]     # Skip Active Record files
  -S, [--skip-sprockets]         # Skip Sprockets files
  -d, [--database=DATABASE]      # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
                                 # Default: sqlite3
  -j, [--javascript=JAVASCRIPT]  # Preconfigure for selected JavaScript library
                                 # Default: jquery
  -J, [--skip-javascript]        # Skip JavaScript files
      [--dev]                    # Setup the application with Gemfile pointing to your Rails checkout
      [--edge]                   # Setup the application with Gemfile pointing to Rails repository
  -T, [--skip-test-unit]         # Skip Test::Unit files
      [--rc=RC]                  # Path to file containing extra configuration options for rails command
      [--no-rc]                  # Skip loading of extra configuration options from .railsrc file

Runtime options:
  -f, [--force]    # Overwrite files that already exist
  -p, [--pretend]  # Run but do not make any changes
  -q, [--quiet]    # Suppress status output
  -s, [--skip]     # Skip files that already exist

Rails options:
  -h, [--help]     # Show this help message and quit
  -v, [--version]  # Show Rails version number and quit

Description:
    The 'rails new' command creates a new Rails application with a default
    directory structure and configuration at the path you specify.

    You can specify extra command-line arguments to be used every time
    'rails new' runs in the .railsrc configuration file in your home directory.

    Note that the arguments specified in the .railsrc file don't affect the
    defaults values shown above in this help message.

Example:
    rails new ~/Code/Ruby/weblog

    This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
    See the README in the newly created application to get going.

rails new

kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle exec rails new . -B -d mysql -T -f
       exist  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
       force  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/assets/images/.keep
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/robots.txt
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep

Gemfile

kenmochi-no-MacBook-Pro:imacros-skill junya$ cat Gemfile
source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'

# Use mysql as the database for Active Record
gem 'mysql2'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

bundler

kenmochi-no-MacBook-Pro:imacros-skill junya$ cat .bundle/config 
---
BUNDLE_PATH: vendor/bundle
BUNDLE_DISABLE_SHARED_GEMS: '1'
kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.0) 
Using i18n (0.6.4) 
Using minitest (4.7.5) 
Using multi_json (1.7.7) 
Using atomic (1.1.10) 
Using thread_safe (0.1.2) 
Using tzinfo (0.3.37) 
Using activesupport (4.0.0) 
Using builder (3.1.4) 
Using erubis (2.7.0) 
Using rack (1.5.2) 
Using rack-test (0.6.2) 
Using actionpack (4.0.0) 
Using mime-types (1.23) 
Using polyglot (0.3.3) 
Using treetop (1.4.14) 
Using mail (2.5.4) 
Using actionmailer (4.0.0) 
Using activemodel (4.0.0) 
Using activerecord-deprecated_finders (1.0.3) 
Using arel (4.0.0) 
Using activerecord (4.0.0) 
Using bundler (1.3.5) 
Installing coffee-script-source (1.6.3) 
Installing execjs (1.4.0) 
Installing coffee-script (2.2.0) 
Using thor (0.18.1) 
Using railties (4.0.0) 
Installing coffee-rails (4.0.0) 
Using hike (1.2.3) 
Installing jbuilder (1.5.0) 
Installing jquery-rails (3.0.4) 
Installing json (1.8.0) 
Installing mysql2 (0.3.13) 
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /Users/junya/.rbenv/versions/2.0.0-p247/bin/ruby extconf.rb 
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/junya/.rbenv/versions/2.0.0-p247/bin/ruby
	--with-mysql-config
	--without-mysql-config
	--with-mysql-dir
	--without-mysql-dir
	--with-mysql-include
	--without-mysql-include=${mysql-dir}/include
	--with-mysql-lib
	--without-mysql-lib=${mysql-dir}/
	--with-mysqlclientlib
	--without-mysqlclientlib
	--with-mlib
	--without-mlib
	--with-mysqlclientlib
	--without-mysqlclientlib
	--with-zlib
	--without-zlib
	--with-mysqlclientlib
	--without-mysqlclientlib
	--with-socketlib
	--without-socketlib
	--with-mysqlclientlib
	--without-mysqlclientlib
	--with-nsllib
	--without-nsllib
	--with-mysqlclientlib
	--without-mysqlclientlib
	--with-mygcclib
	--without-mygcclib
	--with-mysqlclientlib
	--without-mysqlclientlib


Gem files will remain installed in /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13 for inspection.
Results logged to /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/ext/mysql2/gem_make.out

An error occurred while installing mysql2 (0.3.13), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.13'` succeeds before bundling.

MySQLインストールしていなかった。。。→ http://dev.mysql.com/downloads/mysql/
インストール後、以下を実施。

kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.0) 
Using i18n (0.6.4) 
Using minitest (4.7.5) 
Using multi_json (1.7.7) 
Using atomic (1.1.10) 
Using thread_safe (0.1.2) 
Using tzinfo (0.3.37) 
Using activesupport (4.0.0) 
Using builder (3.1.4) 
Using erubis (2.7.0) 
Using rack (1.5.2) 
Using rack-test (0.6.2) 
Using actionpack (4.0.0) 
Using mime-types (1.23) 
Using polyglot (0.3.3) 
Using treetop (1.4.14) 
Using mail (2.5.4) 
Using actionmailer (4.0.0) 
Using activemodel (4.0.0) 
Using activerecord-deprecated_finders (1.0.3) 
Using arel (4.0.0) 
Using activerecord (4.0.0) 
Using bundler (1.3.5) 
Using coffee-script-source (1.6.3) 
Using execjs (1.4.0) 
Using coffee-script (2.2.0) 
Using thor (0.18.1) 
Using railties (4.0.0) 
Using coffee-rails (4.0.0) 
Using hike (1.2.3) 
Using jbuilder (1.5.0) 
Using jquery-rails (3.0.4) 
Using json (1.8.0) 
Installing mysql2 (0.3.13) 
Using tilt (1.4.1) 
Using sprockets (2.10.0) 
Using sprockets-rails (2.0.0) 
Using rails (4.0.0) 
Installing rdoc (3.12.2) 
Installing sass (3.2.10) 
Installing sass-rails (4.0.0) 
Installing sdoc (0.3.20) 
Installing turbolinks (1.3.0) 
Installing uglifier (2.1.2) 
Your bundle is complete!
It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:

<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
kenmochi-no-MacBook-Pro:imacros-skill junya$ 

rails server (動かない!)

kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle exec rails server
/Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `require': dlopen(/Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib (LoadError)
  Referenced from: /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle
  Reason: image not found - /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2/mysql2.bundle
	from /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.13/lib/mysql2.rb:8:in `<top (required)>'
	from /Users/junya/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `require'
	from /Users/junya/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:72:in `block (2 levels) in require'
	from /Users/junya/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `each'
	from /Users/junya/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:70:in `block in require'
	from /Users/junya/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `each'
	from /Users/junya/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler/runtime.rb:59:in `require'
	from /Users/junya/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/bundler-1.3.5/lib/bundler.rb:132:in `require'
	from /Users/junya/Dropbox/repos/imacros-skill/config/application.rb:12:in `<top (required)>'
	from /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:76:in `require'
	from /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:76:in `block in <top (required)>'
	from /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `tap'
	from /Users/junya/Dropbox/repos/imacros-skill/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'

解決法
http://stackoverflow.com/questions/10557507/rails-mysql-on-osx-library-not-loaded-libmysqlclient-18-dylib

kenmochi-no-MacBook-Pro:imacros-skill junya$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

rails server

kenmochi-no-MacBook-Pro:imacros-skill junya$ bundle exec rails s
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-08-01 01:49:35] INFO  WEBrick 1.3.1
[2013-08-01 01:49:35] INFO  ruby 2.0.0 (2013-06-27) [x86_64-darwin12.4.0]
[2013-08-01 01:49:35] INFO  WEBrick::HTTPServer#start: pid=76708 port=3000

localhost:3000にアクセスするとMySQLのエラーが発生するが、まだDB作ってないので当然。
ということで、ひとまず完了!