簡體   English   中英

Rails 4:如何使用載波上傳多個圖像

[英]Rails 4: How to upload multiple images using carrierwave

我可以使用carrierwave上傳文件,如以下代碼所示。 如何編輯代碼,使一篇文章最多可以上傳3個文件的圖像?

意見\\文章\\ new.html.erb

.
.
<div class="span8">
    <%= render 'shared/article_form' %>
</div>
.
.

views \\ shared \\ _article_form.html.erb

.
.
<%= form_for(@article) do |f| %>
  <div class="field">
  <%= f.hidden_field :category_id %>
  <%= f.fields_for :photos do |p| %>
    <%= p.hidden_field :article_id %>
    <% if p.object.image and p.object.image.file %>
      <%= image_tag p.object.image.thumb.url %>
      <p><%= p.object.image.file.filename %></p>
    <% end %>
    <%= p.file_field :image %>
  <% end %>
  <%= f.text_area :content, placeholder: "Enter contents..." %>
  </div>
  <%= f.submit class: "btn btn-large btn-primary" %>
<% end %>
.
.

\\型號\\ article.rb

class Article < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos
  .
  .
end

\\型號\\ photo.rb

class Photo < ActiveRecord::Base
  belongs_to :article
  mount_uploader :image, ImageUploader
  validates :image, presence: true
end

\\控制器\\ articles_controller.rb

class ArticlesController < ApplicationController
  .
  .
def new
  @article = Article.new
  @category = Category.find(params[:category])
  @article.category_id = @category.id
  @article.photos.build
end

def create
  @article = current_user.articles.build(article_params)
  if @article.save
    flash[:success] = "article created!"
    redirect_to current_user #root_url
  else
    @feed_items = []
    render 'new'
  end
end

def edit
  @article = Article.find(params[:id])
end

def update
  @article = Article.find(params[:id])
  if @article.update(article_params)
    redirect_to current_user
  else
    render 'edit'
  end
end
  .
  .
private

  def article_params
    params.require(:article).permit(:content, :category_id, photos_attributes: [:id, :article_id, :image])
  end
  .
  .
end

將您的新操作更改為此:

def new
  @article = Article.new
  @category = Category.find(params[:category])
  @article.category_id = @category.id
  @photo = @article.photos.build
end

並且在您的file_field中,您需要使用multiple option以便您的表單如下所示:

<%= form_for(@article,:html => { :multipart => true }) do |f| %>
  // article fields
  <%= f.fields_for @photo do |p| %>
    <%= p.file_field :image, multiple: true %>
  <% end %>
<% end %>

更新

更改新動作並制作一張照片3次

def new
  @article = Article.new
  @category = Category.find(params[:category])
  @article.category_id = @category.id
  3.times { @article.photos.build } 
end

和你的表格

<%= form_for(@article,:html => { :multipart => true }) do |f| %>
  // article fields
  <%= f.fields_for :photos do |p| %>
    <%= p.file_field :image %>
  <% end %>
<% end %>

另外,您還必須稍微修改模型以拒絕空白值

class Article < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos, reject_if: proc { |attributes| attributes[:image].blank? }
  .
  .
end

您只需在form添加:html => { :multipart => true }即可上傳多個文件。

<%= form_for(@article,:html => { :multipart => true }) do |f| %>

而且,您還必須將<%= p.file_field :image %>更改為<%= p.file_field :image, :multiple => true %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM