Ruby on rails 3 Rails 3与Carrierwave和Simple Form的多态关联
我正在尝试为照片上传建立一个多态关联,这些照片上传是使用Carrierwave处理的。我使用简单表单来构建表单。我觉得关联是正确的,所以我想知道我的问题是否只是表单或控制器的问题 以下是我的联想: property.rb:Ruby on rails 3 Rails 3与Carrierwave和Simple Form的多态关联,ruby-on-rails-3,activerecord,carrierwave,polymorphic-associations,simple-form,Ruby On Rails 3,Activerecord,Carrierwave,Polymorphic Associations,Simple Form,我正在尝试为照片上传建立一个多态关联,这些照片上传是使用Carrierwave处理的。我使用简单表单来构建表单。我觉得关联是正确的,所以我想知道我的问题是否只是表单或控制器的问题 以下是我的联想: property.rb: class Property < ActiveRecord::Base attr_accessible :image ... has_many :image, :as => :attachable ... end 来自properties/_fo
class Property < ActiveRecord::Base
attr_accessible :image
...
has_many :image, :as => :attachable
...
end
来自properties/_form.html.erb的代码段
<%= f.input :image, :label => 'Image:', :as => :file %>
我已尝试更新我的表单以使用字段_,正如我在其他博客中看到的那样:
<%= 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 %>
“Photo”,:as=>:file%>
:隐藏%>
:隐藏%>
:隐藏%>
:文件%>
我所知道的是,我要把这件事做好,真是费了好大劲。我对Rails非常陌生,所以即使调试它也很困难。调试器在3.2中不起作用也无济于事:(因为您的模型有很多:图像(应该是:图像,而不是:图像),所以您希望在视图中使用嵌套的表单。您应该在单元和属性模型上为:图像设置接受嵌套的属性,并将可访问的属性从:图像更改为:图像属性
查看有关如何使用它的良好指南。效果非常好!我唯一需要做的额外事情是在property.rb中设置attr\u accessible:images\u attributes。
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
<%= f.input :image, :label => 'Image:', :as => :file %>
undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x00000102291bb8>
{"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"}
Can't mass-assign protected attributes: image
<%= 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 %>