2011-03-10 42 views
1

我試圖在我的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}。線索?提前致謝。

+0

考慮將Paperclip附件的名稱更改爲除模型本身的名稱以外的其他名稱。你也不需要'attr_accessible',因爲Paperclip已經提供了訪問這些信息的方法。 – Eric 2011-03-10 19:55:12

回答

0

這裏是我這個星期早些時候用在Rails3中的代碼 - 在我的模型

has_attached_file :image1 

沒有attr_accessible

,然後我起來遷移將字段添加到模型看起來像

add_column :offers, :image1_file_name, :string 
add_column :offers, :image1_content_type, :string 
add_column :offers, :image1_file_size, :integer 
add_column :offers, :image1_updated_at, :datetime 

希望能幫助並提供一些見解?看起來像你可能會錯過你的字段名稱中的圖像名稱?

+0

謝謝。但是,這是將一張照片添加到單色模型的方法,而不是具有其他具有一張照片的模型的has_many模型。它的汽車模型在將具有'has_attached_file'鉤子的任意數量的car_photos保存到回形針時遇到了麻煩。 – Scott 2011-03-10 18:14:29