`
moqi1212
  • 浏览: 4207 次
  • 性别: Icon_minigender_2
  • 来自: Boston
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

阅读更多
任务A:产品维护
我们第一个任务就是创建一个产品维护的界面,里面主要是有新建、修改和删除某一个产品的功能。
在这里我们主要分为一下几步来完成:
1、搭建基于rails架构的一个应用项目,项目名为depot
2、创建depot项目所需的数据库,这里使用的数据库为mysql
3、在mysql的数据库中创建数据表,用于存放depot的产品
4、运行所创建的产品维护界面
5、修改刚才所创建的数据表——添加字段
6、再次修改数据表格——添加权限
7、美化产品维护网页的界面

一、搭建depot项目的整体框架
因为rails自2.0以后,其默认数据库就改为了sqlite3,而不是以前所用的mysql了,所以要使用mysql数据库的话,那么在创建应用项目的时候,就应该把要需要的数据库标注在其后面。即在命令行中输入:
rails depot –d mysql
此时会在当前目录中自动生成一个depot的目录,这就是我们depot项目的整体框架,我们后续的工作都要在这个目录中完成。所以,需要进入depot目录中,即在当前目录下输入:
cd depot

二、创建depot项目的数据库
在depot目录下,继续使用命令行命令,输入:
rake db:create:all
这个命令就是创建所有的数据库,此时在mysql数据库中会生成三个关于depot项目的数据库,它们是depot_development(开发数据库,用于存放我们真正的数据)、depot_production、depot_test(测试数据库,用于测试的)
当然,我们也可以只建立一个数据库,可以使用如下命令:
rake db:create RAILS_ENV=’development’
再使用如下命令:
rake db:migrate
至此,depot项目的数据库已经搭建好了。

三、在已创建的数据库中创建数据表
项目搭建起来了,数据库也建立起来了,接着,我们所要做的就是创建数据表了,在这里我们先创建一个用于存放产品的数据表product,在这个数据表中我们有如下几个字段:
  title :string (产品名,字符型)
  description :text (描述,文本型)
  image_url :string (图标,字符型)
那么,我们就需要继续在depot目录中输入命令:
ruby script/generate scaffold product title:string description:text image_url:string
    此时,就会在depot_development数据库中创建一个名为products数据表,其中有title,description和image_url三个字段。
再运行rake db:migrate命令

四、运行已经建立起来的产品维护网页
我们如何可以看到我们所建立起来的产品维护这个网页呢?我们可以使用如下命令:
ruby script/server
然后,我们再在IE浏览器中输入:
http://127.0.0.1:3000/products 或者是 http://localhost:3000/products
这时,我们就可以在网页中看到我们所建立起来的产品维护界面,在这里可以添加、修改和删除某一个产品。

五、修改刚才创建的数据表
刚才我们所创建的存放产品的数据表中,少了价格这一项,所以我们需要添加价格price字段,则我们可以用如下命令:
ruby script/generate migration add_price_to_product price:decimal
这时候,在db/migrate目录下,会自动生成一个add_price_to_product.rb文件,我们需要修改这个文件,所以打开此文件
class AddPriceToProduct < ActiveRecord::Migration
def self.up
add_column :products, :price, :decimal,
# 在products表中增加一个price字段,类型是decimal型
:precision => 8, :scale => 2, :default => 0
#这是我们需要增加的代码,precision是说price可以是8位数,scale是说小数点后精确到2位,default指默认值为0
End

def self.down
remove_column :products, :price
end
end
    我们再运行rake db:migrate命令完成此次修改

这时候,我们刷新我们的网页,会发现,新添加的price字段不在网页中,这是因为刚才我们只是修改了数据库中的数据表,但是没有修改网页,下面我们需要来修改网页。在这里,我们需要修改app/views/products目录下的index.html.erb、edit.html.erb、new.html.erb和delete.html.erb四个文件。
首先,我们来修改index.html.erb文件
<h1>Listing products</h1>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Image url</th>
<th>Price</th>  #table里增加Price这个字段
</tr>
<% for product in @products %>
<tr>
<td><%=h product.title %></td>
<td><%=h product.description %></td>
<td><%=h product.image_url %></td>
<td><%=h product.price %></td> #存放product中的price
<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 %>
   其次,我们再修改new.html.erb文件
   <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, :rows => 6 %>
</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 %>

  接着,我们再修改edit.html.erb文件
  <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 %>
  
最后,再修改show.html.erb文件
<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 %>
最后,再刷新网页,就可以看到price字段了。

六、再次修改数据表——添加权限
在刚才的网页中,我们什么都不添,直接点create按钮,会发现照样可以在数据库中添加数据,也就是说,我们的数据表并没有做任何权限限制,这样会有很多潜在的问题,为此我们需要添加权限。
在这里,我们添加如下权限:
1、title、description、image_url三个字段不能为空(因为在前面添加price字段时,已经对price做了修改,也就是说,在什么都不填的情况下,其默认值为0)
2、price字段只能填写数值,也就是说除了数值以外,其他的都不允许通过
3、price的值不能小于0.01,因为没有一本书的价格会小于0.01元
4、title字段必须唯一,即不能有重复(为了减少数据的冗余度)
5、image_url字段中只能是.jpg或.png或.gif格式

首先,我们打开app/models/product.rb文件,里面只有
class Product < ActiveRecord::Base
end
现在,我们需要在这个里面添加相应的权限限制代码:
1、title、description、image_url三个字段不能为空
       validates_presence_of :title, :description, :image_url
2、price字段只能填写数值
       validates_numericality_of :price      
3、price的值不能小于0.01
       validate :price_must_be_at_least_a_cent
(使price_must_be_at_least_a_cent有效,这个price_must_be_at_least_a_cent是我们需要自定义的一个方法(method),而这个方法不能被外面使用,所以这里需要用被
保护protected)
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
4、title字段必须唯一
        validates_uniqueness_of :title
5、image_url字段中只能是.jpg或.png或.gif格式
       validates_format_of :image_url,
                          :with => %r{\.(gif|jpg|png)$}i,
                          :message => 'must be a URL for GIF, JPG or‘+
                                      ’PNG image.'

七、美化产品维护网页的界面
最后,我们需要来修饰下我们产品维护的界面,让它显得比较美观些。
       ruby script/generate migration add_test_data
然后修改db/migrate目录下的add_test_data.rb文件
    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)
     #创建一个数据,标题为:Pragmatic Version Control,描述为:%{}里面的内容,图标为:images目录下的svn.jpg文件,价格为:28.50
   # . . .
   End
   def self.down
     Product.delete_all
   end
end
接着,再使用rake db:migrate命令
再接着,修改app/views/layouts/products.html.erb文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
   <title>Products: <%= controller.action_name %></title>
   <%= stylesheet_link_tag 'scaffold' ,’depot’%>
</head>
<body>
    <p style="color: green"><%= flash[:notice] %></p>
<%= yield %>
</body>
</html>
再修改app/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 %>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics