我有一个rails应用程序,我试图使用nested_attributes上传多个图像,但遇到了很大困难。这些图片似乎没有储存给mongodb。我已经按照本教程(尽管似乎缺乏详细信息):http://initializd.com/blog/2013/3/upload-multiple-files-with-html5-rails-mongoid-paperclip-and-google-cloud-storage
基本上,我有一个表单,用户可以提交多个列表(作为嵌套属性),并且在每个列表下,他们可以提交多个图像(也作为嵌套属性)。
这是我的表单:
重要的部分是file_field_tag,用户应该上传多个图像。
<%= nested_form_for @user, url: wizard_path, :html => { :multipart => true } do |f| %>
<h1 style="margin-bottom: 6%;"> Add Listings </h1>
<!-- nested fields -->
<%= f.fields_for :listings do |builder| %>
<div class="field form-group">
<%= builder.label :type %> <br />
<%= builder.select :type, options_for_select(listing_types), {}, { :class => 'form-control', :required => 'true' } %>
</div>
<div class="field form-group">
<%= builder.label :title %> <br />
<%= builder.text_field :title, class: 'form-control', required: 'true' %> <br />
</div>
<div class="field form-group">
<%= builder.label :description %> <br />
<%= builder.text_area :description, class: 'form-control', required: 'true' %>
</div>
<div class="field form-group">
<%= builder.label :url %> <br />
<%= builder.text_field :url, class: 'form-control', required: 'true' %>
</div>
<div class="field form-group">
<%= builder.label :zipcode %> <br />
<%= builder.text_field :zipcode, class: 'form-control', size: '24x4', required: 'true' %>
</div>
<div class="field form-group">
<%= label_tag "upload images" %> <br />
<%= file_field_tag('listing_images_attributes_file', multiple: true, name: "user[listings_attributes][images_attributes][][image]", :required => 'true') %>
</div>
<div class="field form-group">
<%= builder.link_to_remove "Remove this listing", :onclick =>"myFunction(this)" %>
<hr style="height: 2px; background-color: lightgray;">
</div>
<% end %>
<p style="margin-bottom: 15px;"><%= f.link_to_add "Add a listing", :listings %></p>
<div class="actions">
<%= f.submit "Continue", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
以下是我的模特:
清单模型:
class Listing
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :type, type: String
field :description, type: String
field :url, type: String
field :zipcode, type: String
# association between listing and user
embedded_in :user
# each listing has many images
embeds_many :images, :cascade_callbacks => true
accepts_nested_attributes_for :images, :allow_destroy => true
end
图片模型:
class Image
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paperclip
field :listing_id, type: Integer # what is the purpose of this field exactly?
embedded_in :listing
has_mongoid_attached_file :image,
:path => ":rails_root/public/images/:id/:filename",
:url => "/images/:id/:filename"
end
来自用户控制器更新操作的片段(基本上是我的创建):
def update
@user = current_user
#have to include steps to validate zip code
case step
when :personal
@user.update_attributes(user_params)
when :listings
# should I build a listing as a default input?
# listing = @user.listings.build
@user.update_attributes(user_params)
end
render_wizard @user
end
def user_params
params.require(:user).permit(:first_name, :last_name, :zipcode, :bio, listings_attributes: [:title, :type, :description, :url, :zipcode,
{ images_attributes: [:image] }, :_destroy, :id])
end
基本上我试图让用户能够上传多个列表,然后在每个列表下面显示多个图像。我想知道这是否可以使用nested_attributes或我是否必须手动处理params?还想知道我的强力参数是否设置正确。这是我的服务器日志中的一个片段,用于显示我的参数的样子:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"HUpgUfukx3Rqb2Ye3su1/9ts4byC1BFrB3HOLXgQeJr8Vrcf9JyQYvR52FC5psLPZGD2x2DlzmlQRRgEXQWo2g==", "user"=>{"listings_attributes"=>{"0"=>{"type"=>"Airbnb", "title"=>"test", "description"=>"test", "url"=>"test", "zipcode"=>"test", "_destroy"=>"false", "id"=>"57a2b9ba9cd25d2d94110543"}}}, "listing"=>{"images_attributes"=>[{"image"=>#<ActionDispatch::Http::UploadedFile:0x000001016c2b20 @tempfile=#<Tempfile:/var/folders/vp/4hffkvd52gd317mgfqczqspm0000gn/T/RackMultipart20160804-11668-1fjlflq.jpg>, @original_filename="event1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"listing[images_attributes][][image]\"; filename=\"event1.jpg\"\r\nContent-Type: image/jpeg\r\n">}]}, "commit"=>"Continue", "id"=>"listings"}
对于长篇描述感到抱歉,但我真的希望有人能回答这个问题。非常感谢你!