我試圖在我的Rails3應用程序中實現Paperclip和使用艾默生拉基的railscast(http://www.emersonlackey.com/article/paperclip-with-rails -3)作爲模型,因爲它非常類似於我期望做的事情(對於特定模型有多張照片)。但是,我沒有得到任何兒童對象來保存除NULLS之外的任何關於圖像的內容。回形針更新數據庫與NULLS的文件名,文件大小等Rails3
這裏是父模型,汽車,其中有多個車的照片:
class Car < ActiveRecord::Base
attr_accessible :model_year, :admin_model_name_id, :chassis_number, :location, :description, :for_sale, :sale_contact_info_visible_to_all, :sale_contact_number, :car_photos_attributes
has_many :car_photos, :dependent => :destroy
accepts_nested_attributes_for :car_photos, :allow_destroy => true
然後是模型車的照片:
class CarPhoto < ActiveRecord::Base
belongs_to :car
has_attached_file :car_photo, :styles => { :large => "640x480", :medium => "300x300>", :thumb => "100x100>" }
attr_accessible :file_name, :content_type, :file_size, :car_id
end
在保存一個新的車型,該車保存罰款像以前一樣,但CarPhotos不。下面是使用WEBrick輸出:
AREL (0.2ms) INSERT INTO `car_photos` (`file_name`, `content_type`, `file_size`, `car_id`, `created_at`, `updated_at`) VALUES (NULL, NULL, NULL, 38, '2011-03-10 17:15:52', '2011-03-10 17:15:52')
[paperclip] Saving attachments.
[paperclip] Saving attachments.
SQL (5.8ms) COMMIT
Redirected to http://127.0.0.1:3000/cars/38
Completed 302 Found in 144ms
我希望這事做在兩個模型的attr_accessible宏,但我一直沒能確定它到底是什麼。艾默生的源代碼與創建方法的腳手架沒有任何變化,因此不確定是否需要更新我的子項才能讓孩子保存表單中的值。在我看來,我的form_for @car中有:html => {:multipart => true}。線索?提前致謝。
考慮將Paperclip附件的名稱更改爲除模型本身的名稱以外的其他名稱。你也不需要'attr_accessible',因爲Paperclip已經提供了訪問這些信息的方法。 – Eric 2011-03-10 19:55:12