我在rails上的ruby上实现了一个带有carrierwave的上传文件系统。目前每个用户都有一个文件,如何在用户上传新文件时添加而不是覆盖?允许用户以这种方式拥有许多文件(每次只上传一个)
这是我的用户控制器:
def update #upload personal file
files = current_user.personal_files
files += params[:user][:personal_files]
current_user.personal_files = files
current_user.save
respond_to do |format|
if @user.save
# copy_file #make a copy of the uploaded file in public/files/data.xml for running in the bat file
format.html { redirect_to :back, notice: 'File was sucessfully uploaded!' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
当我添加一个新文件时,它出现在DB中:[nil," new_file"]。显示为nil并用新的覆盖旧文件。
和我的观点:
<%= form_for current_user, :method => :put, :html => {:multipart => true} do |f| %>
<h4 class="h1-form">Submit your personal performance file in order to WebProcessPAIR analyse your performance problems</h4>
<div class="field-wrap">
<div class="input-group image-preview">
<input type="text" class="form-control txt_color file_text log_placeholder" placeholder="Upload xml file" disabled="disabled">
<span class="input-group-btn">
<span class="btn btn-large btn-default btn-file">
<span class="glyphicon glyphicon-folder-open"></span>
Browse
<%= f.file_field :personal_files, accept: 'text/xml', multiple: true %>
</span>
<%= button_tag(:type => 'submit', :class => 'btn btn-info sbmt', :disabled => true) do %>
<i class="glyphicon glyphicon-share-alt"></i> Upload
<% end %>
</span>
</div>
</div>
<% end %>
答案 0 :(得分:0)
您可以在控制器方法中执行类似的操作。您必须按照here
所述实施多个文件上传files = upload.files # copy the old images
files << params[:upload][:file] # add new file to the files
upload.files = files # assign back
upload.save
答案 1 :(得分:0)
<%= form_for @product, html: { multipart: true } do |f| %>
在form_for中尝试此multiple: true
。希望这会对你有所帮助。