Rails 4:如何使用carrierwave上传多张图片

编程入门 行业动态 更新时间:2024-10-19 23:48:08
本文介绍了Rails 4:如何使用carrierwave上传多张图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以使用carrierwave上传文件,如下面的代码所示.如何编辑代码以在一篇文章中上传最多 3 个文件的图片?

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?

视图\文章\new.html.erb

views\articles\new.html.erb

..

视图\共享\ _article_form.html.erb

views\shared\ _article_form.html.erb

<%= f.submit class: "btn btn-large btn-primary" %><%结束%>..

. . <%= 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

\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

\models\photo.rb

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

\controllers\articles_controller.rb

\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

推荐答案

将您的新操作更改为:

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

并且在您的 file_field 中,您需要使用 multiple option 所以你的表单看起来像这样:

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 %>

更新

改变你的新动作并制作 3 次照片

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

和你的表格

<%= 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

更多推荐

Rails 4:如何使用carrierwave上传多张图片

本文发布于:2023-11-26 02:51:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632343.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   多张   上传   图片   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!