应用rails进行敏捷web开发学习日记--简单修改1
一、增加字段price:decimail
ruby script/generate migration add_column_to_product price:decimail
rake db:migrate
rake db:migrate
修改app\views\products下的所有erb文件,为其增加price字段
index:
<h1>Listing products</h1>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Image url</th>
<th>Price</th>
</tr>
<% @products.each do |product| %>
<tr>
<td><%=h product.title %></td>
<td><%=h product.description %></td>
<td><%=h product.image_url %></td>
<td><%=h product.price %></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 '
show:
<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 '
edit:
<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 '
new:
<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 '
二、数据校验
修改app\models\product.rb为
class Product < ActiveRecord::Base
#检测数据是否为空
validates_presence_of :title,:description,:image_url,:price
#检测数据是否唯一
validates_uniqueness_of :title
#检测数据是否为数值
validates_numericality_of :price
#用正则检验对象
#检查是否为图片
validates_format_of :image_url,
:with => /\w*\.(gif|jpg|png)/,
:message=>"must be a url for a gif,jpg,png, or bmp image"
protected#不可在外部调用
def validate#rails在保存product实例前会自动调用该方法
errors.add(:price,"should be at least 0.01") if price.nil? || price < 0.01
end
关于数据校验更多方法见http://www.cnblogs.com/odbc/archive/2009/05/14/RubyonRailsValidationHelpers.html
三、样式调整
app\views\products\index.html.erb
<% @products.each do |product| %>
<ul class="<%=cycle('list_odd','list_even')%>">#这里使用cycle将循环一次将值设为'list_odd'、'list_even',这样可以实现交替使用背景颜色
<li> <img src="<%=product.image_url %>"/></li>#这里显示图片
<li><%=h(product.title).titleize %></li>#h方法是对内容中的html进行转义,titleize是将每个单词第一个字母换成大写
<li><%=h(product.description).to(20) %></li>#to(20)显示前20个字符,一个汉字算一个字符
<li><%=h product.price %></li>
<li><%= link_to 'Show', product %>
<%= link_to 'Edit', edit_product_path(product) %>
<%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></li>
</ul>
<% end
更多内置方法见http://www.cnblogs.com/odbc/archive/2009/05/14/rubyonrailsactivemethods.html
定义css,这是需要修改app\views\layouts\products.html.erb
<%= stylesheet_link_tag 'depot' %>
其中的depot对应public\stylesheets\目录中的detop.css(自己创建)
修改css文件为
.list_odd{
background: #ddd;
}
.list_even{
background: #fff;
}
background: #ddd;
}
.list_even{
background: #fff;
}
即可看出效果