载波,Rails 4和多次上传 [英] Carrierwave, Rails 4, and Multiple Uploads

查看:77
本文介绍了载波,Rails 4和多次上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直把头撞在墙上,试图使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

ie。从日志中: form-data; name = \ transcription [document] []

有人能在Rails 4中成功完成多个参数强大的上载吗?请分享?

Has anybody successfully accomplished multiple uploads in Rails 4 with strong parameters? If so would you please share?

谢谢...

干杯,

Bill

推荐答案


这是使用载波上传多个图像的解决方案从头开始安装Rails 4

要做这些,只需按照以下步骤操作即可。

rails new multiple_image_upload_carrierwave

在gem文件中

gem 'carrierwave'
bundle install
rails generate uploader Avatar 

创建发布支架

rails g scaffold post title:string

创建附着后支架

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中无需定义强参数,因为您可以在模型和accept_nested_attribute中定义attribute_accessible发布模型,因为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.

这篇关于载波,Rails 4和多次上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆