ruby-on-rails-4 - validates_attachment_content_type - ruby on rails paperclip




Rails 4使用載波進行多個圖像或文件上傳 (4)

如何使用Rails 4和CarrierWave從文件選擇窗口上傳多個圖像? 我有一個post_controllerpost_attachments模型。 我怎樣才能做到這一點?

有人可以提供一個例子嗎? 有沒有簡單的方法呢?


這是從頭開始在rails 4中使用carrierwave上載多個圖像的解決方案

或者你可以找到工作演示: Multiple Attachment Rails 4

只需按照以下步驟操作即可。

rails new multiple_image_upload_carrierwave

在gem文件中

gem 'carrierwave'
bundle install
rails generate uploader Avatar 

創建後腳手架

rails generate scaffold post title:string

創建post_attachment腳手架

rails generate 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)
       end
       format.html { redirect_to @post, notice: 'Post was successfully created.' }
     else
       format.html { render action: 'new' }
     end
   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 %>

   <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 %>
  <%= link_to "Edit Attachment", edit_post_attachment_path(p) %>
<% end %>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

更新表單以編輯附件views / post_attachments / _form.html.erb

<%= image_tag @post_attachment.avatar %>
<%= form_for(@post_attachment) do |f| %>
  <div class="field">
    <%= f.label :avatar %><br>
    <%= f.file_field :avatar %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

修改post_attachment_controller.rb中的更新方法

def update
  respond_to do |format|
    if @post_attachment.update(post_attachment_params)
      format.html { redirect_to @post_attachment.post, notice: 'Post attachment was successfully updated.' }
    end 
  end
end

在rails 3中,不需要定義強參數,因為您可以在模型中定義attribute_accessible,在post模型中定義accept_nested_attribute,因為在rails 4中不推薦使用可訪問屬性。

對於編輯附件,我們無法一次修改所有附件。 所以我們會逐一替換附件,或者您可以按照您的規則進行修改,這裡我只是告訴您如何更新任何附件。


如果我們看一下CarrierWave的文檔,現在其實很簡單。

https://github.com/carrierwaveuploader/carrierwave/blob/master/README.md#multiple-file-uploads

作為示例,我將使用Product作為我想添加圖片的模型。

  1. 獲取主分支Carrierwave並將其添加到您的Gemfile中:

    gem 'carrierwave', github:'carrierwaveuploader/carrierwave'
    
  2. 在預期模型中創建一個列以承載一系列圖像:

    rails generate migration AddPicturesToProducts pictures:json
    
  3. 運行遷移

    bundle exec rake db:migrate
    
  4. 添加圖片到模型產品

    app/models/product.rb
    
    class Product < ActiveRecord::Base
      validates :name, presence: true
      mount_uploaders :pictures, PictureUploader
    end
    
  5. 在ProductsController中將圖片添加到強參數

    app/controllers/products_controller.rb
    
    def product_params
      params.require(:product).permit(:name, pictures: [])
    end
    
  6. 允許您的表單接受多張圖片

    app/views/products/new.html.erb
    
    # notice 'html: { multipart: true }'
    <%= form_for @product, html: { multipart: true } do |f| %>
      <%= f.label :name %>
      <%= f.text_field :name %>
    
      # notice 'multiple: true'
      <%= f.label :pictures %>
      <%= f.file_field :pictures, multiple: true, accept: "image/jpeg, image/jpg, image/gif, image/png" %>
    
      <%= f.submit "Submit" %>
    <% end %>
    
  7. 在您的視圖中,您可以引用解析圖片數組的圖片:

    @product.pictures[1].url
    

如果您從文件夾中選擇多個圖像,那麼訂單將按照從上到下的順序排列。


此外,我想出瞭如何更新多個文件上傳,我也重構了一下。 這個代碼是我的,但你得到了漂移。

def create
  @motherboard = Motherboard.new(motherboard_params)
  if @motherboard.save
    save_attachments if params[:motherboard_attachments]
    redirect_to @motherboard, notice: 'Motherboard was successfully created.'
  else
    render :new
  end
end


def update
  update_attachments if params[:motherboard_attachments]
  if @motherboard.update(motherboard_params)
    redirect_to @motherboard, notice: 'Motherboard was successfully updated.'
  else
   render :edit
  end
end

private
def save_attachments
  params[:motherboard_attachments]['photo'].each do |photo|
    @motherboard_attachment = @motherboard.motherboard_attachments.create!(:photo => photo)
  end
end

 def update_attachments
   @motherboard.motherboard_attachments.each(&:destroy) if @motherboard.motherboard_attachments.present?
   params[:motherboard_attachments]['photo'].each do |photo|
     @motherboard_attachment = @motherboard.motherboard_attachments.create!(:photo => photo)
   end
 end

當使用association @post.post_attachments您不需要設置post_id







carrierwave