原先的産品product模式中存放的是圖檔的url,必須手動将圖檔存入指定目錄中.現在略作改動,在資料庫中建立一個pictures表,其設定如下:
同時配置picture和product的關系,讓product has_one picture,讓picture belongs_to product.然後修改product以前的image_url的驗證模式:
1.删除其必須存在的驗證
2.在其regexp的驗證中加入allow_blank:true
在product中加入after_save鈎子和pic_tmp虛拟屬性:
以便在product儲存中同時更新picture中與product對應的關系.
最後修改視圖中的界面: