发现学习Ruby好辛苦,先说明下 写下这个只是想记录下自己的学习Ruby的艰苦历程,有可能会对其他的初学者有帮助,高手可以不用看了。
今天基本上用了半天才弄完一个helloworld demo. 和以前的版本不兼容是最大的障碍.网上的前辈的经验貌似起不了多大的作用.
就个人而言,喜欢用最新版本的,选择了 1.9.1
RubyInstaller - Previews =>rubyinstaller-1.9.1-p243-preview2.exe
这个双击+next+ Finish 就可以了
安装之后 在环境变量中 加入 RUBY_HOME =C:\Ruby19 (这个是我自定义的安装目录) 别忘记在Path 加入% RUBY_HOME%\bin
接下来到CMD下 运行 ruby -v 会有这个出现
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
装 Rails 这个要去下载 运行 gem install rails
会很慢 好久才会出现 这个命令是自动下载的
Successfully installed actionmailer-2.3.4
Successfully installed activeresource-2.3.4
Successfully installed rails-2.3.4
3 gems installed
Installing ri documentation for actionmailer-2.3.4...
Installing ri documentation for activeresource-2.3.4...
Installing ri documentation for rails-2.3.4...
Updating class cache with 0 classes...
Installing RDoc documentation for actionmailer-2.3.4...
Installing RDoc documentation for activeresource-2.3.4...
Installing RDoc documentation for rails-2.3.4...
你可以 运行 gem list 去查看下 有哪些模块
接下来 成功之后 你可以创建下你的web应用了 进入 D:\ 输入 rails rorweb (这个命令会创建rorweb的目录,假设我的应用文件夹设在D:\rorweb)
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create config/locales
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create test/fixtures
create test/functional
create test/integration
create test/performance
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application_contro
create app/helpers/application_helper.rb
create config/database.yml
create config/routes.rb
create config/locales/en.yml
create db/seeds.rb
create config/initializers/backtrace_sile
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defa
create config/initializers/session_store.
create config/environment.rb
create config/boot.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/dbconsole
create script/destroy
create script/generate
create script/runner
create script/server
create script/plugin
create script/performance/benchmarker
create script/performance/profiler
create test/test_helper.rb
create test/performance/browsing_test.rb
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
进入 rorweb目录,运行 ruby script\generate controller nishijia 会产生一系列的文件
注: nishijia 是我的跳转的控制器的名字 可以随意
exists app/controllers/
exists app/helpers/
create app/views/nishijia
exists test/functional/
create test/unit/helpers/
create app/controllers/nishijia_controller.rb
create test/functional/nishijia_controller_test.rb
create app/helpers/nishijia_helper.rb
create test/unit/helpers/nishijia_helper_test.rb
在D:\rorweb\app\controllers 会生成 nishijia_controller.rb 文件 编辑下加入
class NishijiaController < ApplicationController
def index
render:text => "Hello RoR World"
end
def rails
render:text => "Hello Nishijia"
end
end
注: 以前的Rails版本 是
render_text "Hello RoR World"
现在的版本必须写上
render:text => "Hello RoR World"
否则会遇到 “undefined method `render_text' ”问题.
进入 rorweb目录,运行下 ruby script\server 这个命令会启动server
[2009-11-03 14:30:40] INFO WEBrick 1.3.1
[2009-11-03 14:30:40] INFO ruby 1.9.1 (2009-07-16) [i386-mingw32]
[2009-11-03 14:30:40] INFO WEBrick::HTTPServer#start: pid=5704 port=3000
访问 http://localhost:3000/ 点下那个 About your application’s environment 链接 是否会报错
看后台, 报错是正常的
Status: 500 Internal Server Error
no such file to load -- sqlite3
这个错误的解决方法:我把数据库换成了mysql,没有用Sqlite3 想要装 Splite3 可以运行 gem install sqlite3-ruby
1)首先 运行 gem install rdoc 然后 gem install mysql 否则会 遇到 一大堆的 No definition for 问题
注: Mysql是 5.0的版本 比较稳定
Successfully installed mysql-2.8.1-x86-mingw32
1 gem installed
Installing ri documentation for mysql-2.8.1-x86-mingw32...
Updating class cache with 153 classes...
Installing RDoc documentation for mysql-2.8.1-x86-mingw32...
2)修改D:\rorweb\config\下的database.yml 这个文件是自动产生的 默认是 Sqlite3的数据库配置
# gem install sqlite3-ruby (not necessary on OS X Leopard) development: adapter: mysql encoding: utf8 database: myruby_development username: root password: test host: localhost socket: /opt/local/var/run/mysql5/mysqld.sock #test: # adapter: mysql # encoding: utf8 # database: myruby_test # username: root # password: test # host: localhost # socket: /opt/local/var/run/mysql5/mysqld.sock #production: # adapter: mysql # encoding: utf8 # database: myruby_production # username: root # password: test # host: localhost # socket: /opt/local/var/run/mysql5/mysqld.sock
#是表示注释掉的,上面的 用户名和密码自己配下 database: 填 数据库名 不要是不存在的 否则会有
Status: 500 Internal Server Error
Unknown database 'r4rmusic1_development'
3)接着把 mysql/bin 下的libmySQL.dll copy到 C:\Ruby19\bin 下
然后重起下服务器 点那个查看信息的链接就不会有 报错了
Ruby version 1.9.1 (i386-mingw32)
RubyGems version 1.3.5
Rack version 1.0
Rails version 2.3.4
Active Record version 2.3.4
Active Resource version 2.3.4
Action Mailer version 2.3.4
Active Support version 2.3.4
Application root D:/rorweb
Environment development
Database adapter mysql
Database schema version 0
Getting started
这个信息出来后表示 你的配置成功了
访问下 http://localhost:3000/nishijia 可以显示 Hello RoR World
访问下 http://localhost:3000/nishijia/rails 可以显示 Hello Nishijia
能显示的话表示 第一个demo成功了 辛苦 辛苦