1.Rails初步筆記

完成一個Rails項目的新建,及創建數據庫。

步驟:

環境搭建參考:

http://ruby-china.org/wiki/install_ruby_guide


1.創建項目,指定使用到數據庫

$ rails new depot --database=mysql

看下生成的文件:

$ ls

app     config.ru  doc      Gemfile.lock  log     Rakefile     script  tmp
config  db         Gemfile  lib           public  README.rdoc  test    vendor

查看數據配置文件:

$ cat config/database.yml

# MySQL.  Versions 4.1 and 5.0 are recommended.
# 
# Install the MYSQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: depot_development
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock


# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: depot_test
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock


production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: depot_production
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

查看GemFile:
$cat Gemfile
.......
gem 'mysql2'
......

2.創建數據庫
$ rake db:create RAILS_ENV='development'
$ rake db:migrate
$ mysql -u root
mysql> show databases;
+----------------------+
| Database             |
+----------------------+
| information_schema   |
| depot_development    |
| depot_test           |
| mysql                |
| performance_schema   |
| test                 |
+----------------------+
在執行rake的時候,由於在config/database.yml中指定使用的密碼。
因此,需要讓你輸入密碼。
但是,當命令執行完畢後,登錄mysql的時候,如果使用mysql -u root -p:
$ mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
而是,直接使用mysql -u root,可以登錄到mysql。
菜鳥還不明白....

另外,在生成到工程中到script文件裏,沒有generate。研究中。

在其中遇到了一些問題,有點亂,暫記錄如下:
1.set used ruby version
$rvm list 
rvm rubies

   ruby-1.8.7-p370 [ x86_64 ]
 * ruby-1.9.3-p194 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

$rvm 1.9.3 --default

2.mysql
To use mysql ,u must change config/database.yml,
and also change Gemfile.

3.`resolve': Could not find gem 'mysql (>= 0) ruby
$ rake db:create RAILS_ENV='development'
`resolve': Could not find gem 'mysql (>= 0) ruby...

$ gem install mysql

4.Could not find a JavaScript runtime

$ sudo apt-get install nodejs

5.also mysql 
$rake db:create RAILS_ENV='development'
$rake db:migrate

then use mysql command:
mysql> show databases;
+----------------------+
| Database             |
+----------------------+
| information_schema   |
| db/depot_development |
| db/depot_test        |
| mysql                |
| performance_schema   |
| test                 |
+----------------------+
6 rows in set (0.04 sec)

it's obviously we have made some misktake.

$gedit config/database.yml

del db/

do rake command again.

mysql> show databases;
+----------------------+
| Database             |
+----------------------+
| information_schema   |
| db/depot_development |
| db/depot_test        |
| depot_development    |
| depot_test           |
| mysql                |
| performance_schema   |
| test                 |
+----------------------+
8 rows in set (0.00 sec)

depot_development
depot_test 

this is what we want.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章