52ky 发表于 2022-5-2 13:19:52

RUBY ON RAILS 实用圣经阅读 (3)

由于是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'
解决办法
# 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的架构,必须关联数据库,用 railsfirst 创建会默认关联 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 库,具体怎么设置还没学习到

(Because it is 1.8 x; The Bible's 1.9.3 is too poor, so press 1.8 X to create Hello World
First hello world!!
1. Create project rails - D MySQL first
2。 Create controller
cd first
ruby script/generate controller hello
3. Create interactive actions
vi app/controllers/hello_ controller. rb
Change to
class HelloController < ApplicationController
def there
end
end
4. Create the view layer corresponding to the action
vi app/views/hello/there. rhtml
<h1>Hello, World!</ h1>
5. Start the server at this time
./script/server
5. Front end access
http://192.168.60.128:3000/hello/there
report errors
Errno::ENOENT in HelloController#there
No such file or directory - /tmp/mysql. sock
Solution: after installing MySQL
service mysqld start
terms of settlement:
ln -s /var/lib/mysql/mysql. sock /tmp/mysql. sock
Problems encountered
#42000Unknown database 'first_ development'
terms of settlement
[ 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)
Finally, the TMD visit succeeded
Summary:
1. Due to the architecture of Ruby on rails, the database must be associated. Creating with rails first will associate SQLite library by default. Remove database YML also reports an error
2. Use rails - D MySQL first to create the meeting in config / database Write in 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
It indicates that the development library is used in the current development, and the production library can be used after going online. I haven't learned how to set it
)



页: [1]
查看完整版本: RUBY ON RAILS 实用圣经阅读 (3)