■
昨日作ったモデルにバリデーション処理を追加する
class Product < ActiveRecord::Base validates_presence_of :title, :description, :image_url validates_numericality_of :price validates_uniqueness_of :title validates_format_of :image_url, :with => %r{\.(gif|jpg|png)$}i, :message => "is not GIF,JPG,PNG IMAGE" protected def validate errors.add(:price, "is Not Correct") if price.nil? || price < 0.01 end end
Productのモデルにデータを追加する画面は、
今までScaffoldを使用してきたけども、viewの部分を変更するために
Scaffoldを生成する
# script/generate scaffold product admin exists app/controllers/ exists app/helpers/ exists app/views/admin exists app/views/layouts/ exists test/functional/ dependency model exists app/models/ exists test/unit/ exists test/fixtures/ skip app/models/product.rb identical test/unit/product_test.rb identical test/fixtures/products.yml create app/views/admin/_form.rhtml create app/views/admin/list.rhtml create app/views/admin/show.rhtml create app/views/admin/new.rhtml create app/views/admin/edit.rhtml overwrite app/controllers/admin_controller.rb? [Ynaqd] Y force app/controllers/admin_controller.rb overwrite test/functional/admin_controller_test.rb? [Ynaqd] Y force test/functional/admin_controller_test.rb identical app/helpers/admin_helper.rb create app/views/layouts/admin.rhtml create public/stylesheets/scaffold.css