`

Ruby on Rails之InstantRails使用

阅读更多
Ruby on Rails之InstantRails使用

進入網站 http://instantrails.rubyforge.org/wiki/wiki.pl?Instant_Rails 
選擇[Download] 下載 Instant Rails 1.3
http://instantrails.rubyforge.org/wiki/wiki.pl?Getting_Started 裡有安裝的詳細內容
解壓至C:\InstantRails , 點選InstantRails.exe
會自動檢查 apache, mysql, phpmyadmin (Rails裡亦包含這些)

http://localhost/   進入網站
http://localhost/mysql/   進入phpmyadmin管理mysql server
(mysql default user為root, 沒有password)

點選 Configure > Windows Hosts file
加入
127.0.0.1 www.mycookbook.com
127.0.0.1 typo

點選 Rails Applications > Manage Rails Applications...
然後選擇 cookbookc 並按下 "Start SCGI" button.
輸入 http:\\www.mycookbook.com\ 進入網站

在玩Rails前,先來認識一下Ruby:
在windows os下, 進入 cd\InstantRails\ruby\bin
執行:ruby -v
查看ruby版本
執行:irb
進入Ruby shell可進行邏輯運算
執行:irb --simple-prompt
然後輸入 print('Joeyta') 就可看到輸出

建主c:\joeyta.rb, 內容為:
print ("My name is:")         # 括孤可有可無
puts "Joeyta"                 # puts的輸出自動輸行
print "What is your name?\n"  # double quote可解釋escape字元
puts 'Peter Chan\n'           # single quote直接輸出所有字元
print '1+1 ='; puts 1+1       # 傳回 1+1=2
puts "abc"=="abc"             # 傳回true
a = "ab"; b = "ab"
puts "ab".eql?"ab"            # 傳回true,判斷實際值
puts "ab".equal?"ab"          # 傳回false,判斷參考位置
puts 10 > 50                  # 傳回false
puts "abcd".index('c')        # 傳回2

name = "joeyta"; 
printf("名: %s\n", name)      # printf可作格式化
printf("是否joeyta? %s\n", (name == 'joeyta' ? '是' : '否'))

number=gets.to_i              # gets取後輸入,to_i轉成數字,給變數number
puts number                 

if number == 1         # if表達式
 puts '輸入為1'
elsif number == 2
 puts '輸入為2'
else
 puts '輸入不為1,2'
end

unless number == 1            # number不為1時為true
 puts '輸出不為1'
else
 puts '輸出為1'
end

case number                   # case 表達式
 when 1
  puts 'case 1'
 when 2
  puts 'case 2'
 else
  puts 'case 1,2'
end

for i in 0..2
   print i,"\n"
end

for element in [0.2, 4, 'joeyta']   
 print "#{element}\t(#{element.class})\n"
end

(0..2).each {|i| puts i}
(9..12).each do |i| puts i end
2.times {puts "joeyta"}
3.times do |i| puts "peter" end

j = 1000
begin
 j -= 1
 puts j
 if j==997
  break
  end
end while j>=995               # 亦可使用until

(1..5).each do |num|
 print num   
 if num == 4         
  break                      # 亦可使用redo繼續, next下一個, retry重試迴路
 end                        
end


values = [2, 4, 6, 8, 10]
values.length.times do |index|   
print values[index], " "
end

ary = Array.new(3).fill { "foo" }
ary[0].replace "bar"
p ary

執行
c:\InstantRails\ruby\bin>ruby c:\joeyta.rb

玩完Ruby後,現在來玩一下Rails:
c:\InstantRails\ruby\bin>rails C:\InstantRails\rails_apps\mybook
就會在C:\InstantRails\rails_apps\ 目,建主mybook的application及相關的檔案

執行
ruby C:\InstantRails\rails_apps\mybook\script\server
或到Instant Rails > I > Rails Application > Manage Rails Applications
點選mybook 及 按 "Start with WEBrick"
就會啟動網站
輸入http://127.0.0.1:3000/ 進入網站

執行ruby C:\InstantRails\rails_apps\mybook\script\generate controller MyTest
編輯C:\InstantRails\rails_apps\mybook\app\controllers\my_test_controller.rb 為
class MyTestController < ApplicationController
 def index
   render_text "Hello World"
 end
end

輸入 http://127.0.0.1:3000/My_Test/ 就能看到 Hello World

繼續編輯 C:\InstantRails\rails_apps\mybook\app\controllers\my_test_controller.rb
class MyTestController < ApplicationController
 def index
   render_text "Hello World"
 end
 def hello
   render_text "Hello Rails"
 end
end

輸入 http://127.0.0.1:3000/My_Test/hello  就看到 Hello Rails


建立資料庫:
進入 http://localhost/mysql/

執行:
create database mybook;
create table books(
id int(11) auto_increment primary key,
title varchar(100),
description text,
buydate date)

修改 C:\InstantRails\rails_apps\mybook\config\database.yml 
(YAML配置檔,詳情可參考 http://www.yaml.org/http://www.ruby-doc.org/core/classes/YAML.html)
development:
  adapter: mysql
  database: mybook
  username: root
  password:
  host: localhost
test:
  adapter: mysql
  database: mybook
  username: root
  password:
  host: localhost
production:
  adapter: mysql
  database: mybook
  username: root
  password:
  host: localhost
 
執行
ruby C:\InstantRails\rails_apps\mybook\script\generate model book
就會在C:\InstantRails\rails_apps\mybook\app\models 下產生 book.rb
Rails智能地把Book mapping 至 mysql 的books table.
(創建model book就會將Book映射至英文眾數的book talbe,即books table)


執行
ruby C:\InstantRails\rails_apps\mybook\script\generate controller book
編輯 C:\InstantRails\rails_apps\mybook\app\controllers\book_controller.rb
class BookController < ApplicationController
 scaffold:book      # scaffold:book 生成CRUD代碼
end

輸入 http://127.0.0.1:3000/book/new
不可思意地竟然產生了UI 讓用戶新增 修改 刪除 數據到mysql books table.

編輯 C:\InstantRails\rails_apps\mybook\app\controllers\book_controller.rb
class BookController < ApplicationController
 scaffold:book
 def list
end
當輸入 http://127.0.0.1:3000/book/new 會出現缺少template的錯誤頁面

新增
C:\InstantRails\rails_apps\mybook\app\views\book\list.rhtml 內容為
<html>
<head>
<title>All books</title>
</head>
<body>
<h1>Online Mybook - All books</h1>
<table border="1">
<tr>
 <td width="80%"><p align="center"><i><b>book</b></i></td>
 <td width="20%"><p align="center"><i><b>Date</b></i></td>
</tr>
<% @books.each do |book| %>
<tr>
 <td><%= link_to book.title, :action => "show", :id => book.id %></td>
 <td><%= book.buydate %></td>
</tr>
<% end %>
</table>
<p><%= link_to "Create new book", :action => "new" %></p>
</body>
</html>

修改 C:\InstantRails\rails_apps\mybook\app\controllers\book_controller.rb
class BookController < ApplicationController
 scaffold:book
 def list
  @books = Book.find_all
 end
end

輸入 http://127.0.0.1:3000/book/list 就會出現自定的list template


進入 http://localhost/mysql/
use mybook;
create table categories(
 id int(11) auto_increment primary key,
 name varchar(50)
);
alter table books add category_id int(11) not null after description;
INSERT INTO `categories` VALUES (1, '小說');
INSERT INTO `categories` VALUES (2, '科幻');
INSERT INTO `categories` VALUES (3, '漫畫');
INSERT INTO `books` VALUES (1, '天海', '天海一閣', 1, '2006-04-28');
INSERT INTO `books` VALUES (2, '好書', '好書一本', 2, '2006-04-29');


執行
ruby C:\InstantRails\rails_apps\mybook\script\generate model category
ruby C:\InstantRails\rails_apps\mybook\script\generate controller category

修改 C:\InstantRails\rails_apps\mybook\app\model\book.rb
class Book < ActiveRecord::Base
 belongs_to :category
end

修改 C:\InstantRails\rails_apps\mybook\app\model\category.rb
class Category < ActiveRecord::Base
 has_many :books
end

修改 C:\InstantRails\rails_apps\mybook\app\controllers\book_controller.rb
class BookController < ApplicationController
 scaffold:book
 def list
  @books = Book.find_all
 end
 def edit
  @book = Book.find(@params["id"])
  @categories = Category.find_all
 end 
end


新增C:\InstantRails\rails_apps\mybook\app\views\book\list.rhtml 內容為
<html>
<head>
<title>Edit book</title></head>
<body>
<h1>Edit book</h1>
<form action="../update" method="POST">
 <input id="book_id" name="book[id]" size="30" type="hidden" value="<%= @book.id %>" />
 <p><b>Title</b><br>
  <input id="book_title" name="book[title]" size="30" type="text" value="<%= @book.title %>" /> </p>
 <p><b>Description</b><br>
   <input id="book_description" name="book[description]" size="30" type="text" value="<%= @book.description %>" /> </p>
 <p><b>Category:</b><br>
 <select name="book[category_id]">
  <% @categories.each do |category| %>
   <option value="<%= category.id %>" <%= ' selected' if category.id == @book.category.id %>> <%= category.name %>
   </option>
  <% end %>
 </select></p>
 <input type="submit" value="Update" />
</form>
 <a href="/book/show/<%= @book.id %>"> Show </a> | <a href="/book/list"> Back </a>
</body>
</html>

分享到:
评论

相关推荐

    ruby on rails 101

    引用自Nathan Torkington的话:“使用Ruby on Rails就像观看功夫电影一样,看似弱小的新手框架却能够用各种创造性的方式打败众多强大的对手。”这句话生动地描述了Ruby on Rails的独特之处以及它在Web开发领域的影响...

    ruby on rails 教程

    **描述解析:**“台湾朋友写的ruby on rails教程”表明此教程由台湾地区作者撰写,这可能意味着文档中会使用简体中文,并且可能包含针对中文环境或台湾地区特定需求的指导建议。 #### 环境准备:跨平台安装与配置 ...

    Ruby On Rails开发从头来系列教程(chm)

    摘要:一直想尝试Ruby On Rails,但是因为对apache,mysql都不熟,对Rails的环境搭建更是没信心,所以一直没有开始,从知道了InstantRails后,终于在windows上搭建了Ruby On Rails开发环境,开始了Rails的学习。...

    Ruby on Rails 初体验--北大青鸟教师专题讲座PPT

    为了开发RoR应用,开发者可以选择各种开发环境,如InstantRails智能安装包,或者单独安装Ruby、Rails和MySQL,还可以使用如RadRails、NetBeans或Eclipse等IDE工具。 通过一个简单的通讯录应用为例,可以快速体验RoR...

    InstantRails-2.0-win 下载

    它集成了Ruby编程语言、Ruby on Rails框架、SQLite数据库和Webrick服务器等核心组件,使得开发者能够在Windows系统上便捷地搭建并运行Rails应用。 1. **Ruby编程语言**:Ruby是一种面向对象的、动态类型的编程语言...

    InstantRails v2.0 for windows.zip

    InstantRails,一个All In One的套件,可以帮助你快速搭建Ruby On Rails开发环境。 InstantRails自带了apachet和mysql,软件解压即可使用,如果已经在本机运行了apache和mysql,需要更改端口才能便之并存。

    rails2.0的配置方法

    Rails 2.0作为Ruby on Rails(简称ROR)框架的一个重要版本,在Web开发领域具有不可忽视的地位。本篇将详细介绍Rails 2.0的配置过程及注意事项,帮助初学者快速上手并深入理解该版本的核心功能。 #### 二、Rails ...

    NetBeans 6.0 安装和配置Ruby Support

    - 可选地,你可以勾选`JRuby and Rails Distribution`以安装JRuby和Ruby on Rails框架。 - `Ruby Experimental Hints`提供额外的源代码提示,可以根据需要选择。 - 安装过程包括点击`Install`,接受许可协议,...

    如何在局域网安装Redmine(原创)

    Redmine是一款基于Web的项目管理和跟踪工具,由Ruby on Rails (RoR)框架编写而成。它为团队提供了全面的功能,包括但不限于问题追踪、任务管理、版本控制集成、时间追踪、文件管理等。Redmine的设计初衷是为了提供一...

    RoR 培训课程PPT

    - **课程性质**:本课程为为期五天的Ruby on Rails(简称RoR)入门级培训,适合初学者快速掌握RoR的基本概念和技术要点。 - **讲师信息**:由Peter Marklund开发并讲解,Peter是一位经验丰富的Ruby on Rails开发者。...

    如何在局域网安装Redmine

    - InstantRails(例如,2.0-win版本),这是一个Ruby on Rails开发环境的集合,包含了Ruby、Rails、SQLite等组件,便于快速搭建Redmine环境 ### 3. 安装步骤 1. 安装InstantRails,按照安装向导完成设置。 2. 解压...

    redmine-0.8.2

    Redmine 是一个基于 Ruby on Rails 开发的开源项目管理软件,它提供了问题跟踪、项目管理和文档协作等功能。在本主题中,我们关注的是"redmine-0.8.2"这个特定版本,它是在InstantRails-2.0环境中配置使用的。 ...

    redmine 1.2.1 安装文档

    Redmine 是一款开源的项目管理软件,基于 Ruby on Rails 开发,具有问题追踪、项目文档管理、时间跟踪等多种功能。本文将详细介绍如何在Windows XP环境下安装 Redmine 1.2.1,包括所需软件的下载、安装和配置步骤。 ...

    FreeMIS-开源

    Ruby on Rails(简称 Rails)是一个基于 Ruby 语言的开源 web 应用框架,遵循 MVC(Model-View-Controller)设计模式。Rails 强调“约定优于配置”,提供了丰富的内置功能和简洁的语法,使得开发者能够快速构建功能...

    Redmine安装实践

    **Redmine**是一款基于Web的项目管理和问题跟踪工具,采用Ruby on Rails (ROR)框架进行开发,支持跨平台运行,并兼容多种数据库系统。其设计初衷是为了解决团队协作中的需求管理、任务分配、文档共享等问题,提供了...

    Redmine1.2 安装指南

    接下来,你需要安装Ruby编程语言环境,因为Redmine是基于Ruby on Rails框架构建的。虽然现在最新版本的RailsInstaller集成了Ruby 1.9.2,但Redmine 1.2.1需要的是Ruby 1.8.7。因此,你需要找到并安装适合Redmine ...

    Windows下Redmine-1.2.1的安装(补充)

    - 创建服务:使用 `ruby script/server -e production` 命令启动Redmine服务,并将该命令注册为Windows服务。 #### 四、总结 通过以上步骤,您应该已经在Windows环境下成功安装并配置好了Redmine-1.2.1。Redmine的...

Global site tag (gtag.js) - Google Analytics