Rails 4: How to upload multiple images using carrierwave

匿名 (未验证) 提交于 2019-12-03 09:58:14

问题:

I can upload a file with using carrierwave as shown in the following code. How can I edit the code to make it possible to upload images up to 3 files in one article?

views\articles\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 %> . .

\models\article.rb

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

\models\photo.rb

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

\controllers\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

回答1:

Change your new action to this:

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

and in your file_field you need to use multiple option so your form will look like this:

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

Update

Change your new action and build a photo 3 times

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

and your form

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

Also you'll have to modify your model a bit too in order to reject blank values

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


回答2:

You just have to add :html => { :multipart => true } to your form to upload multiple files.

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

And also you have to change this line <%= p.file_field :image %> to <%= p.file_field :image, :multiple => true %>



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!