Agile Web Development with Rails第六章笔记——任务A:创建应用程序

25 篇文章 0 订阅

看书效率比较低,很容易忘记前面的内容,从这章开始写读书笔记。希望可以早日上手Rails生气

本章的主要任务是以增量式开发的方式创建应用程序Depot(任务A)。

迭代A1:创建商品维护的应用程序

创建能够对商品进行增删改的应用程序。

商品属性products表:title(名称)、description(描述)、image(图)、price(价格)

1、创建Rails应用程序

创建名为depot的应用程序

rails new depot


2、生成脚手架

通过脚手架为products表创建模型、视图、控制器和迁移。

rails generate scaffold Product title:string description:text image_url:string price:decimal

rails的脚手架可以快速生成应用程序的一些片段,如果你需要为一个资源创建一系列的控制器视图模型,那么脚手架就是你需要的工具。


生成器生成一批文件,包括迁移文件如下:

3、应用迁移

修改迁移文件,定义价格属性的格式(共有8位有效数字,其中小数点后有2位)


使用rake应用此迁移

rake db:migrate


什么是数据库迁移?

数据库迁移就是使用数据库迁移文件将数据库从一个状态转换到另一个状态的动作。每个迁移任务代表针对数据库进行的一次修改,采用独立于数据库的源程序形式来描述,这就是数据库迁移文件。修改的内容既可能是针对数据库结构的,也可能是针对表中的数据的。我们可以用这些迁移任务来升级(up)数据库,比如说创建一个表;也可以撤销(down)对数据库的工作,比如将创建的表删掉。

4、查看商品清单

至此,应用程序已被创建,下面就可以启动服务器来看看界面了

启动服务器:rails server

浏览器输入url+控制器名称(products)显示商品列表页面


选择New product添加商品


编辑商品进行保存

        

的确很神奇,通过4个命令就已经完成了许多工作,一行代码都木有写就有了能够运行的程序偷笑

PS.

程序使用SQLite3数据库,在database.yml文件的development部分定义了这个数据库。


注:rake test是Rails根据脚手架生成的单元、功能和集成测试。

迭代A2:美化商品清单

在A1基础上美化界面,并通过图像URL来展示商品的照片。

1、在书中指定的网站上获取测试数据如下:

Product.delete_all
Product.create(:title => 'Web Design for Developers',
  :description => 
    %{<p>
        <em>Web Design for Developers</em> will show you how to make your
        web-based application look professionally designed. We'll help you
        learn how to pick the right colors and fonts, avoid costly interface
        and accessibility mistakes -- your application will really come alive.
        We'll also walk you through some common Photoshop and CSS techniques
        and work through a web site redesign, taking a new design from concept
        all the way to implementation.
      </p>},
  :image_url =>   '/images/wd4d.jpg',    
  :price => 42.95)
# . . .
Product.create(:title => 'Programming Ruby 1.9',
  :description =>
    %{<p>
        Ruby is the fastest growing and most exciting dynamic language
        out there. If you need to get working programs delivered fast,
        you should add Ruby to your toolbox.
      </p>},
  :image_url => '/images/ruby.jpg',
  :price => 49.50)
# . . .

Product.create(:title => 'Rails Test Prescriptions',
  :description => 
    %{<p>
        <em>Rails Test Prescriptions</em> is a comprehensive guide to testing
        Rails applications, covering Test-Driven Development from both a
        theoretical perspective (why to test) and from a practical perspective
        (how to test effectively). It covers the core Rails testing tools and
        procedures for Rails 2 and Rails 3, and introduces popular add-ons,
        including Cucumber, Shoulda, Machinist, Mocha, and Rcov.
      </p>},
  :image_url => '/images/rtp.jpg',
  :price => 43.75)


将测试数据复制到db目录的seeds.rb文件中,运行rake db:seed命令,将测试数据填充到products表中。



2、拷贝图片,将从链接下载来的图片拷贝到/app/assets/images目录下。


3、拷贝样式表depot.css,将从链接下载来的文件拷贝到/app/assets/stylesheets目录下。


只要拷贝成功,样式表就已经到位了,我们不需要在html页面做出引用。这是因为rails有一个单独的文件来为整个应用程序创建标准的页面环境,这个文件是/app/views/layouts/application.html.erb。这里没有做任何改动,它加载了所有的assets文件夹下的内容了。

4、编辑/app/views/products/index.html.erb来替换由脚手架生成的视图,内容改为

<div id=product_list>

<h1>Listing products</h1>

<table>


<% @products.each do |product| %>

  <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><%= product.title %></dt>

    <dd><%= truncate(strip_tags(product.description), :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 %>

 
</div>

</table>
<br />
<%= link_to 'New Product', new_product_path %>
5、重新访问地址http://localhost:3000/products查看效果。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值