Ruby on Rails 實戰聖經阅读(三)

作者: jlins
发布时间:2015-07-13 11:27:35

 

由于是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 库,具体怎么设置还没学习到

来源:http://www.cnblogs.com/dyllove98/p/3196883.html

推荐: