我试图以嵌套形式在文件字段上使用
HTML5多重属性.
模型如下:@H_301_3@
class Album < ActiveRecord::Base has_many :album_images has_many :images,:through => :album_images accepts_nested_attributes_for :images end class Image < ActiveRecord::Base has_many :album_images has_many :albums,:through => :album_images mount_uploader :filename,ImageUploader validates_presence_of :filename end
风景:@H_301_3@
<%= semantic_form_for @album,:url => upload_path do |f| %> <%= f.inputs do %> <%= f.input :name,:label => 'Album title' %> <% end %> <%= f.input :images,:as => :file,:input_html => {:multiple => true} %> <%= f.buttons do %> <%= f.commit_button 'Upload' %> <% end %> <% end %>
<%= f.input :images,:input_html => {:multiple => true} %>
我得到:@H_301_3@
<input id="album_images" multiple="multiple" name="album[images][]" type="file">
这似乎不是正确的,因为我想我想直接在对象上设置文件名,但我不知道这一点.当我尝试使用此字段上传时,传入的参数如下所示:@H_301_3@
"album"=>{"name"=>"2011-01-09","images"=>["IMG_0052.JPG","IMG_0053.JPG","IMG_0054.JPG","IMG_0055.JPG"]}
ActiveRecord::AssociationTypeMismatch (Image(#2157004660) expected,got String(#2151988680)):
好的,那个错误可能是因为它刚刚收到一个文件名,而不是一个图像对象.所以我用于文件字段:@H_301_3@
<%= f.input :images,:input_html => {:multiple => true,:name => 'album[images][][filename]'} %>
<input id="album_images" multiple="multiple" name="album[images][][filename]" type="file">
传入的参数看起来像:@H_301_3@
"album"=>{"name"=>"2011-01-09","images"=>[{"filename"=>"IMG_0052.JPG"},{"filename"=>"IMG_0053.JPG"},{"filename"=>"IMG_0055.JPG"}]}
Image(#2153868680) expected,got ActiveSupport::HashWithIndifferentAccess(#2158892780)
那么在Rails中如何设置这个多文件输入归档映射呢?@H_301_3@
谢谢.@H_301_3@