尝试在不破坏现有数据保存结构的情况下实现此目的。我有一个表格可以将单张图片上传到资源。 修改修改了5个图片资源的表单。 见下文。 。 。
表格如下:
<%= form_for @asset, :html => { :multipart => true } do |f| %>
<div class="span10">
<%= render 'shared/asset_errors' %>
<h3>Upload to Procedure Folder</h3>
</div>
<div class="span5">
<% @procedures = @patient.procedure_folders.all %>
<%= f.label :procedure_folder_id, "Procedure" %>
<%= f.select :procedure_folder_id, @procedures.map { |p|
[Procedure.find(p.procedure_id).procedure, p.id] } %>
</div>
<div class="span5">
<%= f.label :date %>
<%= f.text_field :date, 'data-behaviour' => 'datepicker' %>
</div>
<div class="span10">
<%= f.label :notes %>
<%= f.text_field :notes %>
</div>
<div class="span10">
<% 5.times do |image| %>
<p><%= f.file_field :asset, name: "asset[asset][#{image}]",
id: "asset_asset_#{image}" %></p>
<% end %>
</div>
<div class="span10">
<%= f.hidden_field :patient_id, value: @patient.id %>
<%= f.submit "Upload", class: "btn btn-primary" %>
</div>
<% end %>
我想要实现的 - 并且已阅读列出的一些方法 - 是静态放置此表单中的5个表单字段..或者使用“添加另一个照片”按钮使其准备好jquery。 / p>
班级和控制人员
第一课:
class Asset < ActiveRecord::Base
attr_accessible :asset, :asset_content_type, :asset_file_name, :asset_file_size, :asset_updated_at, :patient_id, :procedure_folder_id, :date, :notes
belongs_to :procedure_folder
belongs_to :patient
has_attached_file :asset, :styles => { :large => "800x600",
:thumb => "150x150" }
validates :date, presence: true
validates :patient_id, presence: true
validates :asset, :attachment_presence => true
default_scope order: 'assets.date ASC'
end
控制器:
class AssetsController < ApplicationController
before_filter :signed_in_user
def create
@asset = Asset.new(params[:asset])
if @asset.save
flash[:success] = "Images uploaded successfully!"
redirect_to patient_path(@asset.patient_id)
else
flash[:error] = "There was an error, Date and Image is required!"
redirect_to patient_path(@asset.patient_id)
end
end
def destroy
@asset = Asset.find(params[:id])
set_variables
if !current_user.admin?
flash[:error] = "You do not have delete priviledges"
redirect_to procedure_folder_path(@asset.procedure_folder_id)
else
@asset.destroy
flash[:info] = "Image has been deleted and cannot be recovered"
redirect_to procedure_folder_path(@asset.procedure_folder_id)
end
end
def set_variables
end
end
我可以采取哪些步骤来有效地保留现有结构并避免以第二种形式添加。这是在一个小型的生产环境中运行,并且希望避免使用现有数据......(在我们实际使用之前,我们会自然地测试开发)。
任何想法/提示/解决方案将不胜感激。
提前谢谢你。
修改
现在表单正确显示我上传多个图片时收到以下错误。
Paperclip::AdapterRegistry::NoHandlerError in AssetsController#create
No handler found for {"0"=>#<ActionDispatch::Http::UploadedFile:0x007f88c517c230 @original_filename="Screen Shot 2013-02-11 at 11.03.46 PM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"asset[asset][0]\"; filename=\"Screen Shot 2013-02-11 at 11.03.46 PM.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/8k/ctb40yd92w13qyjj3fn0gdnc0000gn/T/RackMultipart20130604-13969-jzz7cl>>, "1"=>#<ActionDispatch::Http::UploadedFile:0x007f88c563bb40 @original_filename="Screen Shot 2013-03-18 at 10.59.47 PM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"asset[asset][1]\"; filename=\"Screen Shot 2013-03-18 at 10.59.47 PM.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/8k/ctb40yd92w13qyjj3fn0gdnc0000gn/T/RackMultipart20130604-13969-1h5qj85>>, "2"=>#<ActionDispatch::Http::UploadedFile:0x007f88c563da30 @original_filename="Screen Shot 2013-03-18 at 11.01.06 PM.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"asset[asset][2]\"; filename=\"Screen Shot 2013-03-18 at 11.01.06 PM.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/8k/ctb40yd92w13qyjj3fn0gdnc0000gn/T/RackMultipart20130604-13969-1xsd0fk>>}
Rails.root: /Users/sandovalg/Documents/rails_projects/autre_dlz
Application Trace | Framework Trace | Full Trace
app/controllers/assets_controller.rb:5:in `new'
app/controllers/assets_controller.rb:5:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"RjRFHgYu/4fHRl0iksOkivVOR4ikIvavvXxoqSoTMW0=",
"asset"=>{"procedure_folder_id"=>"1",
"date"=>"2013-06-04",
"notes"=>"",
"asset"=>{"0"=>#<ActionDispatch::Http::UploadedFile:0x007f88c517c230 @original_filename="Screen Shot 2013-02-11 at 11.03.46 PM.png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"asset[asset][0]\"; filename=\"Screen Shot 2013-02-11 at 11.03.46 PM.png\"\r\nContent-Type: image/png\r\n",
@tempfile=#<File:/var/folders/8k/ctb40yd92w13qyjj3fn0gdnc0000gn/T/RackMultipart20130604-13969-jzz7cl>>,
"1"=>#<ActionDispatch::Http::UploadedFile:0x007f88c563bb40 @original_filename="Screen Shot 2013-03-18 at 10.59.47 PM.png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"asset[asset][1]\"; filename=\"Screen Shot 2013-03-18 at 10.59.47 PM.png\"\r\nContent-Type: image/png\r\n",
@tempfile=#<File:/var/folders/8k/ctb40yd92w13qyjj3fn0gdnc0000gn/T/RackMultipart20130604-13969-1h5qj85>>,
"2"=>#<ActionDispatch::Http::UploadedFile:0x007f88c563da30 @original_filename="Screen Shot 2013-03-18 at 11.01.06 PM.png",
@content_type="image/png",
@headers="Content-Disposition: form-data; name=\"asset[asset][2]\"; filename=\"Screen Shot 2013-03-18 at 11.01.06 PM.png\"\r\nContent-Type: image/png\r\n",
@tempfile=#<File:/var/folders/8k/ctb40yd92w13qyjj3fn0gdnc0000gn/T/RackMultipart20130604-13969-1xsd0fk>>},
"patient_id"=>"1"},
"commit"=>"Upload"}
答案 0 :(得分:1)
考虑到您将记录保存到与表单相同的数据库中,似乎您需要使用accepts_nested_attributes_for
进行折扣,这是我通常使用的Paperclip
一次保存多个记录
您的主要问题似乎是一次保存多条记录:
No handler found for {"0"=>
这表明Rails无法计算保存功能,因为它仅针对单个参数设计。实际上,您必须使用手动SQL查询保存记录,例如:
基本上,你不得不循环遍历params数组,并以一种批量方式保存数据。这是你可以做到的一种方式:
根据其中一个链接,您可以使用 activerecord-import
gem
def create
assets = []
params[:asset].each do |k,v|
assets << Asset.new(params[:asset][k])
end
Asset.import assets
end