[入門][続き2]rails3でコントローラ作成してみました!

( Ruby )

お勉強のつづきです。

rails3フレームワークを指定したディレクトリにインストールします。 コマンドはrailsを利用してもろもろ作成していきます。

[nakajijapan] #rails new test
・
・
・

Using rack-ssl (1.3.2) 
Using rdoc (3.9.4) 
Using thor (0.14.6) 
Using railties (3.1.0) 
Installing coffee-rails (3.1.1) 
Installing jquery-rails (1.0.14) 
Using rails (3.1.0) 
Installing sass (3.1.8) 
Installing sass-rails (3.1.4) 
Using sqlite3 (1.3.4) 
Installing turn (0.8.2) 
Installing uglifier (1.0.3) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

その後、コントローラを導入してみます。 処理内容としては顧客情報のCRUDをやらせましょう。

[nakajijapan] #rails generate scaffold customer name:string email:string address:text                                                                                               [/var/www/spec]
/usr/local/Cellar/ruby/1.9.2-p290/lib/ruby/gems/1.9.1/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV_
      invoke  active_record
      create    db/migrate/20111004120942_create_customers.rb
      create    app/models/customer.rb
      invoke    test_unit
      create      test/unit/customer_test.rb
      create      test/fixtures/customers.yml
       route  resources :customers
      invoke  scaffold_controller
      create    app/controllers/customers_controller.rb
      invoke    erb
      create      app/views/customers
      create      app/views/customers/index.html.erb
      create      app/views/customers/edit.html.erb
      create      app/views/customers/show.html.erb
      create      app/views/customers/new.html.erb
      create      app/views/customers/_form.html.erb
      invoke    test_unit
      create      test/functional/customers_controller_test.rb
      invoke    helper
      create      app/helpers/customers_helper.rb
      invoke      test_unit
      create        test/unit/helpers/customers_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/customers.js.coffee
      invoke    scss
      create      app/assets/stylesheets/customers.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

もろもろできちゃいましたね。これでCRUDの処理系は作成し終わっちゃうことになりました。

お継ぎはrakeっていうコマンドを利用して、顧客テーブルをDBに反映させます。すごい便利ですね。 その前にコマンド群をbrewでインストールしているために、若干パスが通っているかのチェックと あるファイルを要求させないとうまく動かないので入れておきます。

vi /usr/local/bin/rake
require 'rake/dsl_definition'

DB反映

[nakajijapan] #rake db:migrate                                                                                                                                               [/var/www/spec/config]
(in /private/var/www/spec)
rake aborted!
Please install the mysql adapter: `gem install activerecord-mysql-adapter` (mysql is not part of the bundle. Add it to Gemfile.)

Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace)

ひゃっほーー。

・・・・!!!!!

アボンしました。

gemで以下のDBに関するプラグイン郡を入れてみることにします。

[nakajijapan] #sudo gem install activerecord-mysql-adapter                                                                                                                   [/var/www/spec/config]
ERROR:  Could not find a valid gem 'activerecord-mysql-adapter' (>= 0) in any repository
ERROR:  Possible alternatives: activerecord-jdbcmysql-adapter, activerecord-odbc-adapter, activerecord-jdbc-adapter, activerecord-jdbcmssql-adapter, activerecord-jdbch2-adapter

[nakajijapan] #gem install activerecord-jdbcmysql-adapter, activerecord-odbc-adapter, activerecord-jdbc-adapter, activerecord-jdbcmssql-adapter, activerecord-jdbch2-adapter [/var/www/spec/config]
ERROR:  Could not find a valid gem 'activerecord-jdbcmysql-adapter,' (>= 0) in any repository
^CERROR:  Interrupted

[nakajijapan] #gem install activerecord-jdbcmysql-adapter activerecord-odbc-adapter activerecord-jdbc-adapter activerecord-jdbcmssql-adapter activerecord-jdbch2-adapter     [/var/www/spec/config]
Fetching: activerecord-jdbc-adapter-1.2.0.gem (100%)
Fetching: jdbc-mysql-5.1.13.gem (100%)
Fetching: activerecord-jdbcmysql-adapter-1.2.0.gem (100%)
Successfully installed activerecord-jdbc-adapter-1.2.0
Successfully installed jdbc-mysql-5.1.13
Successfully installed activerecord-jdbcmysql-adapter-1.2.0
Fetching: activerecord-odbc-adapter-2.0.gem (100%)
Successfully installed activerecord-odbc-adapter-2.0
Successfully installed activerecord-jdbc-adapter-1.2.0
Fetching: jdbc-jtds-1.2.5.gem (100%)
Fetching: activerecord-jdbcmssql-adapter-1.2.0.gem (100%)
Successfully installed jdbc-jtds-1.2.5
Successfully installed activerecord-jdbcmssql-adapter-1.2.0
Fetching: jdbc-h2-1.3.154.gem (100%)
Fetching: activerecord-jdbch2-adapter-1.2.0.gem (100%)
Successfully installed jdbc-h2-1.3.154
Successfully installed activerecord-jdbch2-adapter-1.2.0
9 gems installed
Installing ri documentation for activerecord-jdbc-adapter-1.2.0...
Installing ri documentation for jdbc-mysql-5.1.13...
Installing ri documentation for activerecord-jdbcmysql-adapter-1.2.0...
Installing ri documentation for activerecord-odbc-adapter-2.0...
Installing ri documentation for activerecord-jdbc-adapter-1.2.0...
Installing ri documentation for jdbc-jtds-1.2.5...
Installing ri documentation for activerecord-jdbcmssql-adapter-1.2.0...
Installing ri documentation for jdbc-h2-1.3.154...
Installing ri documentation for activerecord-jdbch2-adapter-1.2.0...
Installing RDoc documentation for activerecord-jdbc-adapter-1.2.0...
Installing RDoc documentation for jdbc-mysql-5.1.13...
Installing RDoc documentation for activerecord-jdbcmysql-adapter-1.2.0...
Installing RDoc documentation for activerecord-odbc-adapter-2.0...
Installing RDoc documentation for activerecord-jdbc-adapter-1.2.0...
Installing RDoc documentation for jdbc-jtds-1.2.5...
Installing RDoc documentation for activerecord-jdbcmssql-adapter-1.2.0...
Installing RDoc documentation for jdbc-h2-1.3.154...
Installing RDoc documentation for activerecord-jdbch2-adapter-1.2.0...

これも必要らしい(あ、忘れた。。。)

sudo gem install bundler

もう一度DBに反映させてみます。

[nakajijapan] #rake db:migrate                                                                                                                                                      [/var/www/spec]
==  CreateCustomers: migrating ================================================
-- create_table(:customers)
   -> 0.1567s
==  CreateCustomers: migrated (0.1568s) =======================================

ひゃっほー!!

DBでもチェックしてみます。

mysql> desc customers;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | YES  |     | NULL    |                |
| email      | varchar(255) | YES  |     | NULL    |                |
| address    | text         | YES  |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)

できましたね。

簡単にできるのはいいけど、多少の融通きけるものであってほしいけどどうなんでしょうね!!!

次はまた今度。

■参照URL http://www.milligramme.cc/wp/archives/3475 http://stackoverflow.com/questions/4297253/install-mysql2-gem-on-snow-leopard-for-rails-3-with-rv