`
martinyuan
  • 浏览: 58813 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Sample in Rails 2.0

    博客分类:
  • ror
阅读更多

Depot Sample in Rails 2.0, Step 1

关键字: depot, rails2.0

Depot for MySQL5.0, Rails2.0 in WindowXP,
# means comments, Hope you can enjoy it.

Objective: To build the basic skeleton of depot


1. create depot rails project
rails depot

2. edit config/database.yml
development:
  adapter: mysql
  database: depot_development
  username: root
  password: admin
  host: 127.0.0.1

test:
  adapter: mysql
  database: depot_test
  username: root
  password: admin
  host: 127.0.0.1

production:
  adapter: mysql
  database: depot_production
  username: root
  password: admin
  host: 127.0.0.1


3. create database defined in database.yml
rake db:drop:all #drop database defined in database.yml
rake db:create:all #create database defined in database.yml
rake db:version #print database version

#rake db:rollback #rollback database to last version, do not need to type this in this sample

4. create product RESTful
ruby script/generate scaffold product title:string description:text image_url:string
#Remember now that Rails 2.0 is RESTful by default. The only difference here is that the ‘scaffold’ behaves like the ‘scaffold_resource’ we had before, and the old non-RESTful scaffold is gone. You also don’t have the ActionController class method ‘scaffold’ that dynamically populated your empty controller with default actions. So, everything scaffold we do is RESTful now. It will create the usual suspects: Controller, Helper, Model, Migration, Unit Test, Functional Test.
#Please check app/controllers/products_controller.rb for confirm

5. Migrate product into database.
rake db:migrate
#Please check the coressponding database with the table named "products" for confirm

6. start Webrick server.
ruby script/server

7. test depot in web browser.
http://localhost:3000/products
 

Depot Sample in Rails 2.0, Step 2

关键字: rails2.0 depot
Objective: To add a new column in pre-built product

1. create a migration task: add a column for product
ruby script/generate migration add_price

2. edit db/migrate/*****_add_price.rb
class AddPrice < ActiveRecord::Migration

          def self.up

            add_column  :products,  :price, :decimal, :precision => 8,  :scale => 2, :default => 0

          end



          def self.down

            remove_column :products,  :price

          end

        end



3. migrate the new product into database
rake db:migrate
#Please confirm coressponding table in database.

4. rebuild scaffold product
ruby script/destroy scaffold product
ruby script/generate scaffold product title:string price:float description:text image_url:string

#the scaffold product should be automatically rebuilt, but actually it did not on my machine. If someone can help me out, appreciate.

5. start Webrick server.
ruby script/server

6. test depot in web browser.
http://localhost:3000/products

Depot Sample in Rails 2.0, Step 3

关键字: rails2.0 depot
Objective: To add validation for product input

1. add validation for product
class Product < ActiveRecord::Base

          

          validates_presence_of :title, :description, :image_url

          

          validates_numericality_of :price

          

          validate :price_must_be_at_least_a_cent





          validates_uniqueness_of :title





          validates_format_of :image_url,

                              :with    => %r{\.(gif|jpg|png)$}i,

                              :message => 'must be a URL for GIF, JPG ' + 'or PNG image.(gif|jpg|png)'



          protected 

          def price_must_be_at_least_a_cent

            errors.add(:price, 'should be at least 0.01') if price.nil? ||

                               price < 0.01

          end

        end



2. That is it?!  That is it!!! Just try it, you even do not need to restart server!   ,my dear J2EE...
 

Depot Sample in Rails 2.0, Step 4

关键字: rails2.0 depot
Objective: To decorate HTML page

1. add some test data
ruby script/generate migration add_test_data

2. edit db/migrate/*****_add_test_data.rb
class AddTestData < ActiveRecord::Migration

          def self.up

            Product.delete_all



            Product.create(:title => 'Pragmatic Project Automation',

            :description => 

            %{<p>

               <em>Pragmatic Project Automation</em> shows you how to improve the 

               consistency and repeatability of your project's procedures using 

               automation to reduce risk and errors.

              </p>

              <p>

                Simply put, we're going to put this thing called a computer to work 

                for you doing the mundane (but important) project stuff. That means 

                you'll have more time and energy to do the really 

                exciting---and difficult---stuff, like writing quality code.

              </p>},

            :image_url =>   '/images/auto.jpg',    

            :price => 29.95)





            Product.create(:title => 'Pragmatic Version Control',

              :description =>

              %{<p>

                 This book is a recipe-based approach to using Subversion that will 

                 get you up and running quickly---and correctly. All projects need

                 version control: it's a foundational piece of any project's 

                 infrastructure. Yet half of all project teams in the U.S. don't use

                 any version control at all. Many others don't use it well, and end 

                 up experiencing time-consuming problems.

              </p>},

            :image_url => '/images/svn.jpg',

            :price => 28.50)





            Product.create(:title => 'Pragmatic Unit Testing (C#)',

            :description => 

            %{<p>

                Pragmatic programmers use feedback to drive their development and 

                personal processes. The most valuable feedback you can get while 

                coding comes from unit testing.

              </p>

              <p>

                Without good tests in place, coding can become a frustrating game of 

                "whack-a-mole." That's the carnival game where the player strikes at a 

                mechanical mole; it retreats and another mole pops up on the opposite side 

                of the field. The moles pop up and down so fast that you end up flailing 

                your mallet helplessly as the moles continue to pop up where you least 

                expect them.

              </p>},

            :image_url => '/images/utc.jpg',

            :price => 27.75)

          end



          def self.down

            Product.delete_all

          end

        end


#%{...} is same as "", but used for long string(text)

3. insert test data into database
Please delete the db/migrate/*****_add_price.rb first, Or it will be executed and a MySQL exception will be thrown.
rake db:migrate
#Please check coressponding table in database for confirm

4. pick "files.rar" from attachment
copy depot.css into public/stylesheets
copy images into public/images
copy index.html.erb into app/views/products


5. edit stylesheet_link_tag of app/views/layouts/products.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

        <head>

          <meta http-equiv="content-type" content="text/html;charset=UTF-8" />

          <title>Products: <%= controller.action_name %></title>

          <%= stylesheet_link_tag 'scaffold', 'depot' %>

        </head>

        <body>



        <p style="color: green"><%= flash[:notice] %></p>



        <%= yield  %>



        </body>

        </html>



6. try it.
 
 下载:
 
分享到:
评论

相关推荐

    Ruby on Rails 2.0的新特性介绍

    【Ruby on Rails 2.0的新特性介绍】 Ruby on Rails 2.0 是这个流行的Web开发框架的一个重大更新,发布于2007年底。Rails以其快速的版本迭代和创新的功能而闻名,从1.0到2.0的升级也不例外。这次更新带来了许多新...

    rails2.0的配置方法

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

    好用的rails 2.0 Api 文档

    Rails 2.0 API 文档是一个非常宝贵的资源,它为开发者提供了全面的指南,以便于在使用Ruby on Rails 2.0版本时更好地理解和利用其框架功能。Ruby on Rails(简称Rails)是一个开源的Web应用框架,它遵循MVC(模型-...

    ruby on rails2.0本地安装包

    这个“ruby on rails2.0本地安装包”适用于Windows和Linux操作系统,特别是针对网络速度较慢的用户设计,方便他们离线安装Rails 2.0.2版本。 在Rails 2.0版本中,有几个关键性的改进和特性: 1. **ActiveRecord**...

    Rails2.0资料

    ### Rails 2.0 关键知识点详析 #### 一、引言 Rails 2.0作为Ruby on Rails框架的一个重要版本,在其发布时引入了一系列改进与更新,旨在提升开发效率与应用程序性能。该版本虽然没有引入革命性的新特性,但通过对已...

    rails2.0下实现《rubyonrails》中的mybook实例

    在Ruby on Rails 2.0框架下,我们经常会遇到创建和操作数据库的需求。在这个实例中,我们将探讨如何在Rails应用中使用SQLite数据库,一个轻量级且易于上手的数据库管理系统,尤其适合开发阶段。标题提到的“mybook”...

    jRuby On Rails WEB2.0

    《jRuby on Rails WEB2.0》:将Ruby on Rails融入Java平台的实践指南 《jRuby on Rails WEB2.0》是一部由Ola Bini撰写的书籍,深入探讨了如何将Ruby on Rails这一敏捷开源框架与Java平台相结合,以构建高效、灵活的...

    Apress - Practical JRuby on Rails Web 2.0 Projects (Sep 2007)

    《JRuby on Rails Web 2.0 实用项目》 英文PDF + 源码

    Rails API 文档

    Rails 2.0版本在当时是一个重要的里程碑,引入了许多新特性并优化了已有的功能。 在Rails API文档中,你可以找到关于以下关键知识点的详尽解释: 1. **路由(Routing)** Rails的路由系统将HTTP请求映射到控制器的...

    rails 2.0.2 分页 需另外下载插件

    在Ruby on Rails框架中,`Rails 2.0.2`是一个较早的版本,而分页功能在那个时期并不像现在的Rails应用那样内置在框架内。为了实现分页,开发者通常需要安装并使用第三方插件,比如"will_paginate"。这个插件允许你在...

    JRuby on Rails Web 2.0 Projects

    ### JRuby on Rails Web 2.0 Projects:将Ruby on Rails引入Java平台 #### JRuby简介 JRuby是一种Ruby语言的实现,它运行在Java平台上,利用了Java虚拟机(JVM)的强大功能。JRuby使得开发人员能够在Java环境中使用...

    rails2-sample

    从给定的文件信息来看,我们正在探讨的是一本关于Ruby on Rails的书籍,书名为《Simply Rails2》,作者是Patrick Lenz。本书旨在为初学者提供深入理解Ruby on Rails框架的指南,从基础概念到高级主题均有涵盖,是...

    Advance RAils Reciples

    《Advanced Rails Reciples》作为Rails领域的经典续作,由Brad Ediger撰写,并在2008年出版,涵盖了Rails 2.0版本的新特性及高级用法。本书不仅适合已经有一定Rails基础的开发者进阶学习,也适合希望深入了解Rails...

Global site tag (gtag.js) - Google Analytics