我按照carrierwave文档中的说明操作,但我的问题是,当我上传两个或更多文件时,只有最后一张照片会上传到我的数据库中。我按照说明一步一步地使用pg作为我的数据库。我还设法成功安装,序列化和使用:images列首先作为字符串+序列化和json。
这是我的CRUD功能和proj_params
def create
@project = Project.new(proj_params)
@project.user_id = @current_user.id if logged_in?
if @project.save
flash[:success] = "Project Succesfully Added"
redirect_to new_project_path
else
render 'new'
end
end
def edit
@project = Project.find(params[:id])
end
def update
@project = Project.find(params[:id])
if @project.update_attributes(proj_params)
flash[:success] = "Project Information Updated"
redirect_to projects_path
else
render 'edit'
end
end
def destroy
Project.find(params[:id]).destroy
flash[:success] = "Project Entry Deleted"
redirect_to projects_path
end
private
def proj_params
params.require(:project).permit(:name,:description, {avatars: []})
end
这是我的项目模型
我的AvatarUploader
class AvatarUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
end
这是项目模型
class Project < ApplicationRecord
mount_uploaders :avatars, AvatarUploader
serialize :avatars, JSON
belongs_to :user
validates :user_id, presence: true
validates :name, presence:true, length:{maximum:150}
validates :description, presence: true, length:{maximum:1000}
end
项目/新视图
<%= form_for @project, html: { multipart: true } do |f| %>
<%= render 'shared/error_messages_proj'%>
<%= render 'shared/flash_messages' %>
<%= f.label :pictures %>
<%= f.file_field :avatars, multiple: true, accept: "image/jpeg, image/jpg, image/gif, image/png" %>
<br>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<br>
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control cons-txt-area' %>
<br>
<%= f.submit "Add Project", class:"btn black" %>
<% end %>