Rails 4 使用carrierwave 上传多张图片或文件 [英] Rails 4 multiple image or file upload using carrierwave

查看:29
本文介绍了Rails 4 使用carrierwave 上传多张图片或文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Rails 4 和 CarrierWave 从文件选择窗口上传多个图像?我有一个 post_controllerpost_attachments 模型.我该怎么做?

谁能举个例子?有没有简单的方法来解决这个问题?

解决方案

这是在rails 4中使用carrierwave从头开始上传多个图像的解决方案

或者你可以找到工作演示:多附件 Rails 4

只需按照以下步骤操作即可.

rails 新的 multiple_image_upload_carrierwave

在 gem 文件中

gem 'carrierwave'捆绑安装rails 生成上传者头像

创建帖子支架

rails 生成脚手架 post title:string

创建 post_attachment 脚手架

rails 生成脚手架 post_attachment post_id:integer avatar:string耙数据库:迁移

在 post.rb

class Post 

在 post_attachment.rb 中

class PostAttachment <ActiveRecord::Basemount_uploader :avatar, AvatarUploader归属地:发布结尾

在 post_controller.rb

def 显示@post_attachments = @post.post_attachments.all结尾定义新@post = Post.new@post_attachment = @post.post_attachments.build结尾定义创建@post = Post.new(post_params)response_to do |格式|如果@post.saveparams[:post_attachments]['avatar'].each do |a|@post_attachment = @post.post_attachments.create!(:avatar => a)结尾format.html { redirect_to @post,注意:'帖子已成功创建.'}别的format.html { 渲染动作:'新' }结尾结尾结尾私人的def post_paramsparams.require(:post).permit(:title, post_attachments_attributes: [:id, :post_id, :avatar])结尾

在 views/posts/_form.html.erb

<%= form_for(@post, :html => { :multipart => true }) do |f|%><div class="field"><%= f.label :title %><br><%= f.text_field :title %>

<%= f.fields_for :post_attachments do |p|%><div class="field"><%= p.label :avatar %><br><%= p.file_field :avatar, :multiple =>真,名称:post_attachments[头像][]"%>

<%结束%><div class="actions"><%= f.submit %>

<%结束%>

编辑任何帖子的附件和附件列表.在 views/posts/show.html.erb

<%= notice %>

<p><strong>标题:</strong><%=@post.title%></p><% @post_attachments.each 做 |p|%><%= image_tag p.avatar_url %><%= link_to "Edit Attachment", edit_post_attachment_path(p) %><%结束%><%= link_to '编辑', edit_post_path(@post) %>|<%= link_to '返回',posts_path %>

更新表单以编辑附件views/post_attachments/_form.html.erb

<%= image_tag @post_attachment.avatar %><%= form_for(@post_attachment) 做 |f|%><div class="field"><%= f.label :avatar %><br><%= f.file_field :avatar %>

<div class="actions"><%= f.submit %>

<%结束%>

修改post_attachment_controller.rb

中的更新方法

def 更新response_to do |格式|如果@post_attachment.update(post_attachment_params)format.html { redirect_to @post_attachment.post,注意:'帖子附件已成功更新.'}结尾结尾结尾

在 rails 3 中不需要定义强参数,因为您可以在模型和 accept_nested_attribute 中定义 attribute_accessible 以发布模型,因为属性可访问在 rails 4 中已弃用.

对于编辑附件,我们不能一次修改所有附件.所以我们会一一替换附件,或者您可以根据自己的规则进行修改,这里我只告诉您如何更新任何附件.

How can I upload multiple images from a file selection window using Rails 4 and CarrierWave? I have a post_controller and post_attachments model. How can I do this?

Can someone provide an example? Is there a simple approach to this?

解决方案

This is solution to upload multiple images using carrierwave in rails 4 from scratch

Or you can find working demo : Multiple Attachment Rails 4

To do just follow these steps.

rails new multiple_image_upload_carrierwave

In gem file

gem 'carrierwave'
bundle install
rails generate uploader Avatar 

Create post scaffold

rails generate scaffold post title:string

Create post_attachment scaffold

rails generate scaffold post_attachment post_id:integer avatar:string

rake db:migrate

In post.rb

class Post < ActiveRecord::Base
   has_many :post_attachments
   accepts_nested_attributes_for :post_attachments
end

In post_attachment.rb

class PostAttachment < ActiveRecord::Base
   mount_uploader :avatar, AvatarUploader
   belongs_to :post
end

In 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

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

To edit an attachment and list of attachment for any post. In 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 %>

Update form to edit an attachment 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 %>

Modify update method in 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

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.

For edit an attachment we cant modify all the attachments at a time. so we will replace attachment one by one, or you can modify as per your rule, Here I just show you how to update any attachment.

这篇关于Rails 4 使用carrierwave 上传多张图片或文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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