《Agile Web Development with Rails》读书笔记(四)

原创 2007年09月21日 14:44:00

The Depot Application

I’ll take the creating a depot application of books for maintaining and buying as an example. Of course, this application has database operation( I use MySQL here ).

Create database:

depot>mysql –u root create depot_development

After create database, you should modify the configuration in the config/database.yml, set the database name, user name and password.

If you want to make sure the job you just did is succeed or not, you could use

depot >rake db:migrate

if not return the error message, that means the job has succeeded.

Create the Product Model and Table:

I use the migration to create and maintain the database structure.

depot >ruby script/generate model product

this generator create a bunch of files. The two we are interested are the model itself, product.rb, and the 001_create_products.rb.

because I want to use migration to maintain the database, let me write code of creating table in migration files.

work/depot/db/migrate/001_create_products.rb

class CreateProducts < ActiveReacord::Migration

   def self.up

      create_table :products do |t|

        t.column :title, :string

        t.column :description, :text

        t.column :image_url, :string

      end

   end

 

   def self.down

      drop_table :products

   end

end

we tell rake to apply the unapplied migration to the database.

depot>rake db::migrate

if you want to rolling back the migration, just use

depot>rake db::migrate VERSION=0

Create Controller

Because we’re using the MVC architecture, our application need a controller to coordinate the stuff of maintaining the list of products.

depot>ruby script/generate controller admin

after create the controller, all the background work has been finished, we should connect the application with the development database.

depot/app/controllers/admin_controller.rb

class AdminController < ApplicationController

   scaffold :proudct

end

the scaffold tell rails to dynamic generate the application code, if you want to change the view style, using this method would have some problems. In this case, you should use static scaffold. I’ll introduce it later.

Now, you can start up the web server to check the job we just finished.

Add a Missing Column

After we create the table, we may find that we missing a column, then we can add the column in the database directly

mysql>alter table products add column price decimal( 8, 2 )

but we use migration before, using migration to add the new column will give us a version-controlled history of the schema and it is simple to recreate it.

depot>ruby script/generate migration add_price

this cmd creates the 002_add_price.rb in the migrate directory. Open the file and edit it.

depot/db/migrate/002_add_price.rb

class AddPrice < ActiveRecord::Migration

   def self.up

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

   end

 

   def self.down

      remove_column :products :price

   end

end

In order to apply this migration to the database, please remember

depot>rake db::migrate

Refresh your browser, you’ll see the price when you new the product.

Validate

In order to avoid the invalid value put into the table, we should put the validation in the model.

Depot/app/models/product.rb

class Product < ActiveRecord::Base

   validates_presence_of :title, :description, :image_rul

   validates_numericallty_of :price

   validates_uniqueness_of :title

   validates_format_of :image_url,

:width => %r{/.(gif|jpg|png)$}i

                 :message => "must be a URL for a GIF, JPG, or PNG image"

   protected

   def validate

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

   end

end

Rails will automatically call the validate method.

Prettier Listings

If we want to change the view code, we must use the static scaffold.

depot>ruby script/generate scaffold product admin

this cmd checks to make sure we have the model file and create the views files need to display the maintain screens. When it reaches the controller, it detected the admin_controller.rb file has been modified, asking the permission to overwrite the file. The only change we made to this file was adding the scaffold :product line, which we on longer need, so we agree.

After this cmd finished, there are several files has been created in the views directory, we could prettier the views by editing the files.

Stylesheet

On another side, we need to write CSS file to style the presentation, and put this file at some place, tell the browser to fetch it.

All scaffold-generated application use the scaffold.css in the directory public/stylesheets. Rather than alter the file, we create a new file named depot.css and put it in the same directory.

Finally, we need to link the stylesheets into our HTML pages, we are no need to write the reference in the HEAD tag in the rhtml files. Rails keep a separate file to create the environment for the admin pages. The file, called admin.rhtml, is a layout page and live in the views/layout.

The only thing we need do is put the ‘depot’ in the

<%= stylesheet_link_tag ‘scaffold’ %>

to make it change to

<%= stylesheet_link_tag ‘scaffold’, ‘depot’ %>

After this, all the ground work has been finished, you can access the pages to see the efficiency.

 

Agile Web Development with Rails

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/topmvp - topmvpRails is ...
  • topmvp
  • topmvp
  • 2008年08月18日 22:52
  • 320

《Agile Web Development with Rails》读书笔记(一)

        I.              My way of RailsWorking as an embedded system developer, I never know much ab...
  • zqho
  • zqho
  • 2007年09月19日 10:20
  • 408

《Agile Web Development with Rails》读书笔记(二)

 Instant GratificationAfter the Rails environment has been established, I write a simple application...
  • zqho
  • zqho
  • 2007年09月19日 15:06
  • 449

《Agile Web Development with Rails》读书笔记(三)

Make Dynamic PagesIn order to get dynamic pages, I need to embed some ruby code into the *.rhtml in ...
  • zqho
  • zqho
  • 2007年09月19日 17:13
  • 357

Agile Web Development with Rails 翻译(四)

Agile Web Development with Rails 翻译(四)第三章 安装 Rails   在开始写Rails应用程序之前,你需要下载Rails框架并安装它到你的计算机上。你需要在Rub...
  • ajiva
  • ajiva
  • 2006年09月11日 14:39
  • 2086

Agile Web Development with Rails 翻译

1、用户导航到我们应用程序。我们要做是使用本地URL如http://localhost:3000/say/hello。2、Rails分析URL。say部分是“控制器”的名字,所以Rails创建一个Ru...
  • qiqiy
  • qiqiy
  • 2008年09月20日 22:47
  • 221

敏捷开发(Agile Development)简介

      敏捷开发(agile development)是一种以人为核心、迭代、循序渐进的开发方法。在敏捷开发中,软件项目的构建被切分成多个子项目,各个子项目的成果都经过测试,具备集成和可运行的特征...
  • wayne_ran
  • wayne_ran
  • 2007年05月08日 20:16
  • 4269

Agile Web Development wiht Rails 2nd ed

 19.9 Layouts and Components         Layouts 是为了解决页面显示重复:             The average web site,though, h...
  • harbey
  • harbey
  • 2008年10月10日 17:38
  • 419

Agile Web Development with Rails 翻译(十七)

Agile Web Development with Rails 翻译(十七)第九章 任务 D: 结算!   迄今为止,我们已经建立了一个基本的产品管理系统,我们实现一个分类目录,并有一个很好看的商店...
  • ajiva
  • ajiva
  • 2006年09月11日 15:01
  • 2472

Agile Web Development with Rails 翻译(十八)

Agile Web Development with Rails 翻译(十八) 这里,唯一有趣的是和选择列表有关的代码。我们已经假定有效付款选项清单是Order model的一个属性--它将是mode...
  • ajiva
  • ajiva
  • 2006年09月11日 15:04
  • 2218
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:《Agile Web Development with Rails》读书笔记(四)
举报原因:
原因补充:

(最多只允许输入30个字)