[英]Rails accepts_nested_attributes_for Polymorphic strong params
I have an article model that has a polymorphic association with an attachment table. 我有一个与附件表具有多态关联的文章模型。
In the new article form I require the user to be able to upload attachments. 在新的文章表格中,我要求用户能够上传附件。
The code works other than the strong params as I have no idea what to place as the strong params for the polymorphic relationship. 该代码除了强参数之外还可以工作,因为我不知道将什么作为多态关系的强参数。
def create
@article = Article.build(article_params)
@article.user_id = current_user.id
if @article.save
params[:article_attachments]['attachment'].each do |a|
@article.attachments.create!(:file => a)
end
respond_to do |format|
format.html {redirect_to article_path(@article)}
format.js
end
else
render 'new'
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params.require(:article).permit(:content, attachments_attributes: [])
end
The attachment model is structured as follows: 附件模型的结构如下:
Attachment
=> Attachment(id: integer, file: string, attachable_id: integer, attachable_type: string, created_at: datetime, updated_at: datetime)
The nested form is: 嵌套形式为:
<div class="uploader pull-left" >
<%= f.fields_for :attachments do |builder| %>
<%= builder.file_field :file, :multiple => true, accept: 'image/jpeg,image/gif,image/png', name: "discussion_attachments[attachment][]" %>
<% end %>
</div>
The params hash is as follows: params哈希如下:
{"utf8"=>"✓", "authenticity_token"=>"m84LL1D05LCsivXuASukGgcDoVhTvxhVuDThv9Q2iDRS/AMOeMvQArc0mpMZJSsW887R2krWu85Xm1v9+WQ8bQ==", "article"=>{"content"=>""}, "attachments"=>{"file"=>[#<ActionDispatch::Http::UploadedFile:0x007f0918ffb720 @tempfile=#<Tempfile:/tmp/RackMultipart20150810-3911-1rsq9lz.JPG>, @original_filename="YourPhoto_0001.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachments[file][]\"; filename=\"YourPhoto_0001.JPG\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f0918ffb338 @tempfile=#<Tempfile:/tmp/RackMultipart20150810-3911-i1ztf2.JPG>, @original_filename="YourPhoto_0002.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachments[file][]\"; filename=\"YourPhoto_0002.JPG\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"Submit", "controller"=>"articles", "action"=>"create"}
It seems to me that you are trying to add multiple file attachments using Carrierwave . 在我看来,您正在尝试使用Carrierwave添加多个文件附件。
In order for this to work you need to specify the following inside of your article_params
... 为了
article_params
起作用,您需要在article_params
内部指定以下内容...
def article_params
params.require(:article).permit(:content, attachments_attributes: [:id, :attachable_id, :name])
end
As you can see you need to specify 3 params. 如您所见,您需要指定3个参数。 Both
:id
and :attachable_id
are needed to specify the association/relationship and :name
is the name of the file/path. :id
和:attachable_id
都需要指定关联/关系,而:name
是文件/路径的名称。
I hope this helps you out, Tim. 希望这对您有所帮助,蒂姆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.