Ruby on Rails 實戰聖經阅读(三)
?
由于是1.8.x;圣經的1.9.3差太多,所以另外按1.8.X來創建hello world
?
第一個Hello World!!
1. 創建項目rails -d mysql first
2。創建控制器?
cd first
ruby script/generate controller hello
3.創建交互動作
vi app/controllers/hello_controller.rb?
修改為
class HelloController < ApplicationController
? def there
? end
end
4.創建對應動作的view層
vi app/views/hello/there.rhtml
<h1>Hello, World!</h1>
5.此時啟動服務器
./script/server?
5.前端訪問
http://192.168.60.128:3000/hello/there
報錯
Errno::ENOENT in HelloController#there
No such file or directory - /tmp/mysql.sock
?解決: 安裝mysql之后
? service mysqld start
解決辦法:
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
遇到問題
#42000Unknown database 'first_development'
解決辦法
[root@localhost /]# mysql
Welcome to the MySQL monitor. ?Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.52 Qihoo.com
?
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
?
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
?
mysql> create database first_development;
Query OK, 1 row affected (0.00 sec)
終于TMD訪問成功
總結:
1.由于ruby on rails的架構,必須關聯數據庫,用 rails ?first 創建會默認關聯 sqlite庫。去掉 database.yml也報錯
2.用rails -d mysql first創建會在config/database.yml中寫下
# MySQL. ?Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# ? gem install mysql
# On Mac OS X:
# ? sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
# On Mac OS X Leopard:
# ? sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
# ? ? ? This sets the ARCHFLAGS environment variable to your native architecture
# On Windows:
# ? gem install mysql
# ? ? ? Choose the win32 build.
# ? ? ? Install MySQL and put its /bin directory on your path.
#
# And be sure to use new-style password hashing:
# ? http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
? adapter: mysql
? encoding: utf8
? database: first_development
? username: root
? password:
? host: localhost
?
# 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: mysql
? encoding: utf8
? database: first_test
? username: root
? password:
? host: localhost
?
production:
? adapter: mysql
? encoding: utf8
? database: first_production
? username: root
? password:?
? host: localhost
說明當前開發時使用的是development庫,上線后可以改用 production 庫,具體怎么設置還沒學習到
轉載于:https://www.cnblogs.com/dyllove98/p/3196883.html
總結
以上是生活随笔為你收集整理的Ruby on Rails 實戰聖經阅读(三)的全部內容,希望文章能夠幫你解決所遇到的問題。