停了蛮久,继续写..
1.常用命令:
linux环境下:
创建数据库 mysqladmin -u root create depot_development
使用数据库 mysql -u root depot_develope
创建model ruby script/generate model Product
执行创建model的数据库脚本 rake db:migrate
生成脚本 ruby script/generate migration add_price
生成scaffold : ruby script/genearte scaffold model名 controller名
2.如何通过model创建表?
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :title, :string
t.column :description, :text
t.column :image_url, :string
end
end
def self.down
drop_table :products
end
end
附加:增加或者删除某字段:
add_column :products, :price, :integer, :default => 0
remove_column :products, :price
上面的例子中建立的表名为model的复数形式,如products
3.如何实行验证?
(1)validates_presence_of 判断是否为空
(2)validates_numericality_of :price, :only_integer => true 判断是否为数字且仅为整数
(3)自定义validate函数:
protected
def validate
errors.add(:price, "should be positive" ) if price.nil? || price <= 0
end
(4)判断是否重复:
validates_uniqueness_of
(5)判断格式,比如url格式:
validates_format_of :image_url,
:with => %r{\.(gif|jpg|png)$}i,
:message => "must be a URL for a GIF, JPG, or PNG image"
4.链接CSS文件:
<%= stylesheet_link_tag 'scaffold' , 'depot' %>
或者默认:
<%= stylesheet_link_tag 'scaffold' %>
![]() |
谢亚龙逼女足姑娘作检讨(图)
“安静”为啥成裁判口头语?
姚明私下发给刘翔的短信
|
![]() |
曝光:姚明小时候与可爱女生合影(图) 组图:隋菲菲私家相册 率性美感领衔女篮 |
![]() |
![]() |
![]() |


档案
日志
相册
视频








评论
想第一时间抢沙发么?