`
waveeee
  • 浏览: 52518 次
  • 来自: 上海
社区版块
存档分类
最新评论

agile rails depot 1

    博客分类:
  • ruby
阅读更多
appointment: this demo ran in ubuntu, and Rails 2.2.2
the demo comes from agile web development with rails 3
script>>some linux script
mysql>some db command

----------------------------------

step 1: create project
script>>rails --database=mysql depot

step 2: create db
script>>rake db:create RAILS_ENV='development'
script>>rake db:migrate
check your mysql now:
script>>mysql -u root
mysql> show databases;
mysql> show tables;
+-----------------------------+
| Tables_in_depot_development |
+-----------------------------+
| schema_migrations           |
+-----------------------------+
1 row in set (0.00 sec)

step 3
use migrations to create schema
script>>script/generate scaffold product title:string description:text image_url:string
u can see this file,u can vi this file: db/migrate/****_create_products.rb
and then run it
script>> rake db:migrate
check ur db again
mysql> desc products;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| title       | varchar(255) | YES  |     | NULL    |                |
| description | text         | YES  |     | NULL    |                |
| image_url   | varchar(255) | YES  |     | NULL    |                |
| created_at  | datetime     | YES  |     | NULL    |                |
| updated_at  | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+


And now u can command this http://localhost:3000/products to you browser
you can new some products and check your db again

step 4
adding a missing column
script>>script/generate migration add_price_to_product price:decimal
a new migrate will create, change it as below
class AddPriceToProduct < 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

script>>rake db:migrate

step 5
change the view to display price
/views/products/index.html.erb
/views/products/edit.html.erb
/views/products/new.html.erb
/views/products/show.html.erb

step 6
validate models/product.rb

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.'
  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




step 7
now we use migrate to create some test data.
script>>script/generate migration add_test_data

class AddTestData < ActiveRecord::Migration
  def self.up
    Product.delete_all
    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)
                   # . . .
  end
  def self.down
    Product.delete_all
  end
end

script>> rake db:migrate

step 8
make Prettier
add depot.css to public/stylesheets
body { background-color: #fff; color: #333; }

body, p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size:   13px;
  line-height: 18px;
}

pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;
}

a { color: #000; }
a:visited { color: #666; }
a:hover { color: #fff; background-color:#000; }

.fieldWithErrors {
  padding: 2px;
  background-color: red;
  display: table;
}

#errorExplanation {
  width: 400px;
  border: 2px solid red;
  padding: 7px;
  padding-bottom: 12px;
  margin-bottom: 20px;
  background-color: #f0f0f0;
}

#errorExplanation h2 {
  text-align: left;
  font-weight: bold;
  padding: 5px 5px 5px 15px;
  font-size: 12px;
  margin: -7px;
  background-color: #c00;
  color: #fff;
}

#errorExplanation p {
  color: #333;
  margin-bottom: 0;
  padding: 5px;
}

#errorExplanation ul li {
  font-size: 12px;
  list-style: square;
}


change views/layouts/products.html.erb
  <%= stylesheet_link_tag 'depot' %>

change views/products/index.html.erb
<div id="product-list">
<h1>Listing products</h1>
<table>
<% for product in @products %>
<tr class="<%= cycle('list-line-odd', 'list-line-even') %>">
<td>
<%= image_tag product.image_url, :class => 'list-image' %>
</td>
<td class="list-description">
<dl>
<dt><%=h product.title %></dt>
<dd><%=h truncate(product.description.gsub(/<.*?>/,''),
:length => 80) %></dd>
</dl>
</td>
<td class="list-actions">
<%= link_to 'Show', product %><br/>
<%= link_to 'Edit', edit_product_path(product) %><br/>
<%= link_to 'Destroy', product,
:confirm => 'Are you sure?',
:method => :delete %>
</td>
</tr>
<% end %>
</table>
</div>
<br />
<%= link_to 'New product', new_product_path %>


and you can prepare some images in public/images/...
and your browsers again.
  • 大小: 90.5 KB
分享到:
评论

相关推荐

    Agile+Web+Development+with+Rails中文版.pdf

    《Agile Web Development with Rails》是一本经典的Rails开发指南,中文版的出版使得更多的中国开发者能够深入理解并应用敏捷开发方法与Ruby on Rails框架。这本书是Rails开发者的必备参考资料,它详细介绍了如何...

    Agile Web Development with Rails 4

    Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...

    Agile Web Development with Rails

    《Agile Web Development with Rails》(敏捷Web开发:Ruby on Rails)这本书,作为Rails开发新手的教材,强调了敏捷开发方法,并以其帮助开发者建立起一个实用的Web应用。从给出的文件信息来看,这本书正在编写过程...

    Agile Web Development with Rails (PDF)

    #### 1. Rails与敏捷开发 Rails作为一款基于Ruby语言的Web应用框架,自诞生之初就秉持着敏捷开发的理念。它强调代码的简洁性、易读性和快速迭代能力,这些特性与敏捷开发的原则不谋而合。通过Rails,开发者可以迅速...

    Agile Web Development with Rails 1-14节_ppt(老师发的修正版)

    Agile Web Development with Rails 1-14节_ppt(老师发的修正版)

    Agile Web Development With Ruby On Rails第一及第二版

    《Agile Web Development With Ruby On Rails》是两本广受欢迎的书籍,主要涵盖了使用Ruby on Rails框架进行敏捷Web开发的知识。这本书的第1版和第2版分别详细讲解了如何运用敏捷开发方法来构建高效、可扩展且易于...

    Agile Web Development with Rails,Fourth Edition 第四版

    《敏捷Web开发与Rails》第四版是一本专为软件开发者设计的权威指南,全面涵盖了使用Ruby on Rails框架进行敏捷Web应用开发的知识。Rails 3是该版本的重点,它引入了许多新特性和改进,使得开发过程更为高效且灵活。...

    Agile Web Development with Rails 3nd Edition Beta.pdf

    ### 敏捷Web开发与Rails 3:关键知识点解析 #### 一、Rails版本与兼容性 本书《敏捷Web开发与Rails》第三版是基于Rails 2编写的。截至本书印刷时,当前可用的Rails Gem版本为2.1。书中所包含的所有代码均已在该...

    Agile Web Development with Rails 4th edition(敏捷Web开发与Rails:程序指南 第四版)

    1. **ActiveRecord**: ActiveRecord是Rails的核心组件之一,负责处理数据库交互。在第四版中,对ActiveRecord的查询接口进行了优化,提供了更丰富的查询方法,如`pluck`, `exists?`, 和 `includes`,这些都极大地...

    Agile Web Development with Rails for Rails 3.2

    ### Agile Web Development with Rails for Rails 3.2 #### 核心知识点概览 - **Rails 3.2概述** - **敏捷开发方法论** - **Model-View-Controller (MVC) 模式** - **Ruby on Rails基础与高级特性** - **面向对象...

    agile web development with rails 5(英文电子书)

    agile web development with rails 5(英文电子书).............................................................................................................................................................

    Agile Web Development with Rails Final

    ### Agile Web Development with Rails Final: Key IT Knowledge Points #### Introduction: Ruby on Rails Framework Ruby on Rails (often shortened as Rails) is a server-side web application framework ...

    Agile Web Development With Rails 3rdEdition

    1. **Rails版本演变**: - 本书基于Rails 2编写,特别指出,书中的示例代码已在Rails 2.1版本上进行过测试。 - 第二版覆盖了Rails 1.2.6版本,这部分内容在本书中以灰色页眉和页脚标记。 - 逐步迁移过程中,内容...

Global site tag (gtag.js) - Google Analytics