uplaoded上嵌套asstribute資產爲attr_writer我有以下模型結構訪問從文件經由形式
class Asset < ActiveRecord::Base
attr_writer :session_user_id
...
end
class Item < ActiveRecord::Base
has_many :assets, :as => :assetable, :dependent => :destroy
...
end
和想把USER_ID在與資產相關聯的值。我有一個問題關聯一個變量在一個uplodated文件。這裏是post'd數據:
"assets_attributes"=>{"3"=>{"asset"=>#<ActionDispatch::Http::UploadedFile:0x007fd04dde17f8 @original_filename="nautugly.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"menu_item[assets_attributes][3][asset]\"; filename=\"nautugly.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/var/folders/94/slp2488s6nvgg8qq0g0p5c0m0000gn/T/RackMultipart20120323-51480-1lpa754>>,
"description"=>""},...
並且想要訪問資產中的session_user_id。在items_controller,我說:
params[:item][:assets_attributes].each_with_index do |value, key|
value.each do |y|
y.asset.session_user_id=12
end
但我收到錯誤消息:
undefined method `asset' for "3":String
我覺得我已經想盡變化。任何想法如何讓這個工作?
THX
嗯...這似乎並不奏效。我只是想獲得與資產相關的user_id。不應該是這個硬的 – timpone 2012-03-24 03:50:58