《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第十章笔记——任务E:更智能的购物车

本章内容: 修改数据库模式与现有数据诊断和处理错误闪存日志 迭代E1:创建更智能的购物车 问题提出:保存与显示购物车中同一产品的数量 解决方案:修改line_items表,添加描述...
  • lissdy
  • lissdy
  • 2013年06月03日 16:43
  • 1181

Mastering Web Application Development with AngularJS-Packt 2013 读书笔记(不错的好书!)

Angular Zen Batarang Batarang is a Chrome developer tool extension for inspecting the AngularJ...
  • cteng
  • cteng
  • 2017年08月03日 14:29
  • 385

Agile Web Development with Rails 4

  • 2016年06月01日 11:38
  • 9.45MB
  • 下载

《Expert Cube Development with Microsoft SQL Server 2008 Analysis Services》读书笔记第九章:保护Cube(一)

SQL Server 2008中SQL应用系列及BI学习笔记系列--目录索引   对Analysis Services Cube而言,安全,是一个非常重要的问题。在某种形式的安全要求下,Cube中的数...

《Designing with Web Standard(Third Edition)》/《网页重构》读书笔记

---------------------------------------读书笔记·转载请注明---------------------------------------   学习前...
  • Hookc
  • Hookc
  • 2013年11月04日 00:54
  • 896

agile web development with rails2代码

  • 2008年08月05日 14:32
  • 1.83MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:《Agile Web Development with Rails》读书笔记(四)
举报原因:
原因补充:

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