我试图设立上传的照片被使用Carrierwave处理的多态关联。 我用简单的表格,建立自己的形式。 我觉得联想是正确的,所以我想知道如果我的问题是只是一些与窗体或控制器。
这里是我的关联:
property.rb:
class Property < ActiveRecord::Base
attr_accessible :image
...
has_many :image, :as => :attachable
...
end
unit.rb
class Unit < ActiveRecord::Base
attr_accessible :image
...
has_many :image, :as => :attachable
end
image.rb
class Image < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
mount_uploader :image, PhotoUploader
end
properties_controller.rb:
def edit
@property = Property.find params[:id]
@property.image.build if @property.image.empty?
end
def update
@property = Property.find params[:id]
if @property.update_attributes params[:property]
redirect_to admin_properties_path, :notice => 'The property has been successfully updated.'
else
render "edit"
end
end
从属性代码段/ _form.html.erb
<%= f.input :image, :label => 'Image:', :as => :file %>
这里是连接了图像提交时,我得到的错误:
undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x00000102291bb8>
这里是PARAMS:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"lvB7EMdc7juip3gBZD3XhCLyiv1Vwq/hIFdb6f1MtIA=",
"property"=>{"name"=>"Delaware Woods",
"address"=>"",
"city"=>"",
"state"=>"",
"postal_code"=>"",
"description"=>"2 bedroom with large kitchen. Garage available",
"incentives"=>"",
"active"=>"1",
"feature_ids"=>[""],
"user_ids"=>[""],
"image"=>#<ActionDispatch::Http::UploadedFile:0x00000102291bb8 @original_filename="wallpaper-4331.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"property[image]\"; filename=\"wallpaper-4331.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:/tmp/RackMultipart20120608-3102-13f3pyv>>},
"commit"=>"Update Property",
"id"=>"18"}
我到处找对多态关联的帮助,我一事无成。 我见过,看起来非常简单的简单的例子。 有一两件事我注意到的是,它似乎是在很多的例子在我的情况下的has_many关联应该是images
而不是image
。 然而,当我这样做,我得到一个错误:
Can't mass-assign protected attributes: image
我试着更新我的形式使用fields_for正如我在其他博客见过像这样:
<%= f.input :image, :label => "Photo", :as => :file %>
<% f.simple_fields_for :images do |images_form| %>
<%= images_form.input :id, :as => :hidden %>
<%= images_form.input :attachable_id, :as => :hidden %>
<%= images_form.input :attachable_type, :as => :hidden %>
<%= images_form.input :image, :as => :file %>
<% end %>
我只知道我在的时候得到这个工作赫克。 我很新到Rails所以即使调试它是困难的。 它没有帮助调试器并没有真正在3.2工作:(