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

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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics