Carrierwave、Rails 4 和多次上传 [英] Carrierwave, Rails 4, and Multiple Uploads
问题描述
我一直在努力让 Carrierwave、Rails 4 和 Multiple Uploads 一起工作.我可以像在这个项目和许多其他项目中一样正常上传单个文件.
I have been banging my head against the wall trying to get Carrierwave, Rails 4, and Multiple Uploads all working together. I can get a single file upload working just fine as in this and many other projects.
这不是一个嵌套的情况 - 只是简单地上传到一个名为 Transcription 的模型并希望为每个上传的文档创建一个记录.
This is not a nested situation - just simply uploading to a single model called Transcription and wanting to create a record for each document uploaded.
我似乎找不到正确的方法来声明用于载波安装的文档"字段
I cannot seem to find the correct way to declare the "document" field used for the carrierwave mount
mount_uploader :document, DocumentUploader
作为强参数识别的数组.
as an array for the strong parameters to recognize.
我尝试过白名单:whitelisted[:document] = params[:transcription]['document']
,
将文档"声明为数组:
params.require(:transcription).permit(..... ,:document => [])
params.require(:transcription).permit(..... , { document: [] })
这一切看起来更像是我在为嵌套模型声明数组,但我真的希望 Rails 4 的强大参数能够简单地看到由 file_field 创建的文档"数组,:multiple => true
This all seems more like I am declaring the array for a nested model, but I really want Rails 4's strong parameters to simply see the "document" array created by the file_field, :multiple => true
即.来自日志:form-data;name="转录[文档][]
有没有人在 Rails 4 中使用强参数成功完成多次上传?如果有,请分享一下?
Has anybody successfully accomplished multiple uploads in Rails 4 with strong parameters? If so would you please share?
谢谢...
干杯,
比尔
推荐答案
这是使用 rails 4 中的载波从头开始上传多个图像的解决方案
只需按照以下步骤操作即可.
rails new multiple_image_upload_carrierwave
在 gem 文件中
gem 'carrierwave'
bundle install
rails generate uploader Avatar
创建脚手架
rails g scaffold post title:string
创建 post_attachment 脚手架
rails g scaffold post_attachment post_id:integer avatar:string
rake db:migrate
在 post.rb 中
class Post < ActiveRecord::Base
has_many :post_attachments
accepts_nested_attributes_for :post_attachments
end
在 post_attachment.rb 中
class PostAttachment < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
belongs_to :post
end
在 post_controller.rb 中
def show
@post_attachments = @post.post_attachments.all
end
def new
@post = Post.new
@post_attachment = @post.post_attachments.build
end
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
params[:post_attachments]['avatar'].each do |a|
@post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id)
end
format.html { redirect_to @post, notice: 'Post was successfully created.' }
else
format.html { render action: 'new' }
end
end
end
def update
respond_to do |format|
if @post.update(post_params)
params[:post_attachments]['avatar'].each do |a|
@post_attachment = @post.post_attachments.create!(:avatar => a, :post_id => @post.id)
end
end
end
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to @post }
format.json { head :no_content }
end
end
private
def post_params
params.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])
end
在views/posts/_form.html.erb中
<%= form_for(@post, :html => { :multipart => true }) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<%= f.fields_for :post_attachments do |p| %>
<div class="field">
<%= p.label :avatar %><br>
<%= p.file_field :avatar, :multiple => true, name: "post_attachments[avatar][]" %>
</div>
<% end %>
<% if params[:controller] == "post" && params[:action] == "edit" %>
<% @post.post_attachments.each do |p| %>
<%= image_tag p.avatar, :size => "150x150" %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
在views/posts/show.html.erb中
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @post.title %>
</p>
<% @post_attachments.each do |p| %>
<%= image_tag p.avatar_url, :size => "150x150" %>
<%= link_to "Destroy", p, method: :delete %>
<% end %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
在 rails 3 中无需定义强参数,您可以在模型中定义 attribute_accessible 并在模型中定义 accept_nested_attribute 来发布模型,因为在 rails 4 中不推荐使用属性可访问性.
In rails 3 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4.
这篇关于Carrierwave、Rails 4 和多次上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!