我们在products页面中输入一堆数据,下面的页面显示了我们输入了三本关于ruby以及rails的书籍。

image_url列,我们暂且不管它的作用及格式,先随便用一个图片名放进这个位置。
不完美之处就是,我们要建一个网上书店,却没有在书的属性中标明价格,实在是极大的失误。怎么办?我们添加一个方法,把price列放入表中。
>ruby script/generate migration add_price_to_product price:decimal
在depot/db/migrate目录下,变有了一个****_add_price_to_product.rb的文件。(注意,创建product的rb文件也在这里。)
D:\rails\depot>ruby script/generate migration add_price_to_product price:decimal
exists db/migrate
create db/migrate/20081029083525_add_price_to_product.rb
D:\rails\depot>rake db:migrate
(in D:/rails/depot)
== 20081029083525 AddPriceToProduct: migrating ================================
-- add_column(:products, :price, :decimal)
-> 0.2650s
== 20081029083525 AddPriceToProduct: migrated (0.2650s) =======================
D:\rails\depot>
数据迁移后,启动server,打开products页面。
(补充:rails是敏捷的。何谓敏捷,简单来说,你做任何改变,它都能够迅速反应。你在rails中更改源码,修改页面,等等,都不需要重启server,直接刷新一下页面就OK。一般我都是开两个命令行窗口,一个负责输入rails命令,一个开server。)
很遗憾,我们没有看到任何变化。但是我们在mysql数据库中却看到了price列。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| depot_development |
| mysql |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use depot_development;
Database changed
mysql> show tables;
+-----------------------------+
| Tables_in_depot_development |
+-----------------------------+
| products |
| schema_migrations |
+-----------------------------+
2 rows in set (0.00 sec)
mysql> describe 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 | |
| price | decimal(10,0) | YES | | NULL | |
+-------------+---------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql>
原因在于,我们更改了数据库,却没有对views(视图)文件夹里面的页面上面显示的内容进行更改。下一步,我们修改depot\app\views\products中的文件。把price添加进页面。
对照下面的代码,将缺少的部分(示下划线+粗体部分)添加到.html.erb文件中去。
depot/app/views/products/index.html.erb
<h1>Listing products</h1>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Price</th>
<th>Image url</th>
</tr>
<% for product in @products %>
<tr>
<td><%=h product.title %></td>
<td><%=h product.description %></td>
<td><%=h product.price %></td>
<td><%=h product.image_url %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New product', new_product_path %>
<h1>New product</h1>
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', products_path %>
<h1>Editing product</h1>
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>
<p>
<b>Title:</b>
<%=h @product.title %>
</p>
<p>
<b>Description:</b>
<%=h @product.description %>
</p>
<p>
<b>Image url:</b>
<%=h @product.image_url %>
</p>
<p>
<b>Price:</b>
<%=h @product.price %>
</p>
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>

浙公网安备 33010602011771号