欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ruby On Rails开发环境For Windows(更新)

程序员文章站 2022-03-03 08:10:47
...

一、Windows下rails环境安装

很简单一步搞定。。。

1.下载Rails DevKit:railsinstaller-version.exe 下载地址 http://railsinstaller.org/ 

   railsinstaller-2.1.exe下载地址http://download.csdn.net/download/loveanna7/5147502

2.安装rails:执行railsinstaller-version.exe安装rails。傻瓜式安装(有一点要注意,安装路径不要有空格);其他组件也一并会安装上,包括ruby、gem、bundle等。

3.安装完成后点完成的时候,那个界面的复选框选上的话,我们就要进入git设置。会自动打开终端,只要输入姓名和邮箱会自动配置好。

4.配置完成之后终端会自动进入C:\Sites。我们现在可以查看我们安装的环境的版本号:ruby -v;rails -v;gem -v;bundle -v

运行日志:

# Rails Environment Configuration.

Your git configuration is incomplete.
user.name and user.email are required for properly using git and services such
as GitHub ( http://github.com/ ).

 Please enter your name, for example mine is: Wayne E. Seguin
name > Jarry Li
Setting user.name to Jarry Li

 Please enter your email address, for example mine is: wayneeseguin@gmail.com
email > jarry-li@163.com
Setting user.email to jarry-li@163.com
'clip' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

---
git:
  user.name:  Jarry Li
  user.email: jarry-li@163.com
  version:    git version 1.7.9.msysgit.0

ruby:
  bin:        D:/dev/RailsInstaller/Ruby1.9.2/bin/ruby.exe
  version:    ruby 1.9.3p125 (2012-02-16) [i386-mingw32]

rails:
  bin:        D:/dev/RailsInstaller/Ruby1.9.2/bin/rails.bat
  version:    Rails 3.2.1

ssh:
  public_key_location: C:\Documents and Settings\Administrator/.ssh/id_rsa.pub
  public_key_contents: ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxGnWu+Vl8ZuVbEg21/w2s
4UIOFPK9ujkl2r19Ysq2E94ZSvaPfjJj7PkN0619Ci35BtmAu14ChdFBWPLi8PhrZu5o0PSZao0wecXX
v8kcl27iaAIQtnXr9uz3319fZRDgx12V/DBnXD5NFI1BTQJVaPxM6SwblJJCDs/ELgR9Wg0x23t4e33s
z0AXkav4kFKbxlJFDXl8vAbauyLozkd28+jFUni8sFau4eDVkv4XVmFBLPAQPb5rSZ8Pmt8ci6Zj2JEX
9rCFr7qPPMiWgSlb2vC3d8S2Lwk+Ju1P/AqiAubemyZnRq25ar0W7Nx+sKjJpHzskthhwZ93UJkaRpxF
Q== Jarry Li <jarry-li@163.com>


NOTES:

  Your public ssh key (id_rsa.pub) has been automatically generated and copied t
o your clipboard.

C:\Sites>ruby -v
ruby 1.9.3p125 (2012-02-16) [i386-mingw32]

C:\Sites>rails -v
Rails 3.2.1

C:\Sites>gem -v
1.8.16

C:\Sites>bundle -v
Bundler version 1.0.22

 

二、新建应用程序

1.创建一个demo应用程序:cd到工作目录,如:E:\works\ruby,执行rails new demo,会创建一个名为demo的应用程序

2.cd到demo目录

3.启动自带默认的rails服务器:rails server

4.浏览器打开http://localhost:3000

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

 

运行日志:

C:\Sites>cd e:\works\

C:\Sites>e:

E:\works>mkdir ruby

E:\works>cd ruby

E:\works\ruby>rails new demo
      create
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/images/rails.png
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  app/mailers/.gitkeep
      create  app/models/.gitkeep
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  lib/assets
      create  lib/assets/.gitkeep
      create  log
      create  log/.gitkeep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  script
      create  script/rails
      create  test/fixtures
      create  test/fixtures/.gitkeep
      create  test/functional
      create  test/functional/.gitkeep
      create  test/integration
      create  test/integration/.gitkeep
      create  test/unit
      create  test/unit/.gitkeep
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.gitkeep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.gitkeep
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
         run  bundle install
Fetching source index for https://rubygems.org/
Installing rake (10.0.3)
Installing i18n (0.6.4)
Installing multi_json (1.6.1)
Using activesupport (3.2.1)
Installing builder (3.0.4)
Using activemodel (3.2.1)
Using erubis (2.7.0)
Installing journey (1.0.4)
Installing rack (1.4.5)
Installing rack-cache (1.2)
Installing rack-test (0.6.2)
Using hike (1.2.1)
Installing tilt (1.3.5)
Installing sprockets (2.1.3)
Using actionpack (3.2.1)
Installing mime-types (1.21)
Using polyglot (0.3.3)
Installing treetop (1.4.12)
Installing mail (2.4.4)
Using actionmailer (3.2.1)
Using arel (3.0.2)
Installing tzinfo (0.3.37)
Using activerecord (3.2.1)
Using activeresource (3.2.1)
Using bundler (1.0.22)
Installing coffee-script-source (1.6.1)
Installing execjs (1.4.0)
Using coffee-script (2.2.0)
Installing rack-ssl (1.3.3)
Installing json (1.7.7) with native extensions
Installing rdoc (3.12.2)
Using thor (0.14.6)
Using railties (3.2.1)
Using coffee-rails (3.2.2)
Installing jquery-rails (2.2.1)
Using rails (3.2.1)
Installing sass (3.2.7)
Installing sass-rails (3.2.6)
Installing sqlite3 (1.3.7)
Installing uglifier (1.3.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem
is installed.

E:\works\ruby>cd demo

E:\works\ruby\demo>rails server
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
        SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
        This poses a security threat. It is strongly recommended that you
        provide a secret to prevent exploits that may be possible from crafted
        cookies. This will not be supported in future versions of Rack, and
        future versions will even invalidate your existing user cookies.

        Called from: D:/dev/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/ac
tionpack-3.2.1/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `i
nitialize'.

[2013-03-14 10:01:15] INFO  WEBrick 1.3.1
[2013-03-14 10:01:15] INFO  ruby 1.9.3 (2012-02-16) [i386-mingw32]
[2013-03-14 10:01:15] INFO  WEBrick::HTTPServer#start: pid=4728 port=3000

 

三、Hello Rails 

写一个hello world程序试试手,新打开一个终端,并cd到E:\works\ruby\demo

1.用rails生成一个名为say的控制器,用命令:rails generate controller Say hello goodbye

日志信息:

E:\works\ruby\demo>rails generate controller Say hello goodbye
        SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
        This poses a security threat. It is strongly recommended that you
        provide a secret to prevent exploits that may be possible from crafted
        cookies. This will not be supported in future versions of Rack, and
        future versions will even invalidate your existing user cookies.

        Called from: D:/dev/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/ac
tionpack-3.2.1/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `i
nitialize'.

      create  app/controllers/say_controller.rb
       route  get "say/goodbye"
       route  get "say/hello"
      invoke  erb
      create    app/views/say
      create    app/views/say/hello.html.erb
      create    app/views/say/goodbye.html.erb
      invoke  test_unit
      create    test/functional/say_controller_test.rb
      invoke  helper
      create    app/helpers/say_helper.rb
      invoke    test_unit
      create      test/unit/helpers/say_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/say.js.coffee
      invoke    scss
      create      app/assets/stylesheets/say.css.scss

 

 

2.控制器demo/app/controllers/say_controller.rb代码

class SayController < ApplicationController
  def hello
  end

  def goodbye
  end
end

 

可以看到是继承了ApplicationController类,里面有两个空的方法。

3.rails的url请求,浏览器访问:http://localhost:3000/say/hello

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

我们运行rails generate时生成了6个文件1个目录say,这个目录用来存放控制器视图的模板文件,我们创建了say控制器,视图就位于app/views/say中。

默认rails会寻找与当前动力(action)同名的文件中的模板。这里我们修改app/views/say/hello.html.erb模板文件,只要在其中添加HTML代码。

当前代码

<h1>Say#hello</h1>
<p>Find me in app/views/say/hello.html.erb</p>

 

修改之后:

<h1>Hello from Rails</h1>

 

刷新浏览器可看到:

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

4.在模板中添加动态内容:每次显示页面时也显示当前时间。

模板文件用.html.erb后缀,是告诉rails,需要借助ERb系统对文件的内容进行扩展,就像jsp/asp/php一样的动态页面文件。ERb是一个过滤器,包含在rails的默认安装里。

修改app/views/say/hello.html.erb模板,添加显示当前时间:

<h1>Hello from Rails</h1>
<p>
	It is new <%= Time.now %>
</p>

 

刷新浏览器:

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

5.将“获取时间”的方法写在控制器中,视图则只承担显示信息的职责。

对控制器中hello方法进行修改,将时间值放在@time的实例变量中,.html.erb模板中输出实例变量即可。

修改控制器app/controllers/say_controller.rb:

class SayController < ApplicationController
  def hello
  	@time = Time.now
  end

  def goodbye
  end
end

 

修改app/views/say/hello.html.erb模板:

<h1>Hello from Rails</h1>
<p>
	It is new <%= @time %>
</p>

 

刷新浏览器, 只看到时间更新。

 

四、把页面连起来

1.创建应用的时候,还创建一个goodbye的动作,对应的模板app/views/say/goodbye.html.erb,现在用得上了,修改它:

<h1>Goodbye!</h1>
<p>
	It was nice having you here.
</p>

 

2.打开http://localhost:3000/say/goodbye

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

3.把hello和goodbye页面连接起来:现在需要在两个模板页面上添加链接,让其互相跳转。

hello.html.erb添加:

<p>
	Say <a href="/say/hello">Hello</a>!
</p>

 

goodbye.html.erb添加:

<p>
	Say <a href="/say/goodbye">Goodbye</a>!
</p>

 

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

4.现在这样是管用,但是如果把应用程序移到Web服务器的另一个目录下,这些Url就会失效。实际上我们可以使用Rails提供的视图模板中的辅助方法。现在使用辅助方法添加跳转链接。

修改hello.html.erb:

<h1>Hello from Rails</h1>
<p>
	It is new <%= @time %>
</p>
<p>
	Time to say 
	<%= link_to "Goodbye", say_goodbye_path %>!
</p>

 

修改goodbye.html.erb:

<h1>Goodbye!</h1>
<p>
	It was nice having you here.
</p>
<p>
	Say <%= link_to "Hello", say_hello_path %> again.
</p>

 

link_to()是一个方法(在rails中把这样的方法叫做辅助方法(helper),因为它给编写视图模板带来了便利)。这里使用时省略了圆括号。

say_goodbye_path参数是rails预计算过的一个值。

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境

Ruby On Rails开发环境For Windows(更新)
            
    
    博客分类: ruby on rails railsruby on railsRORWindows下RubyOnRails开发环境