试图在不破坏现有数据保存结构的情况下实现这一点。我有一个将单个图像上传到资产的表单。编辑修改了 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 准备就绪。
类和控制器
第一课:
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
我可以采取哪些步骤来有效地保留现有结构并避免添加第二种形式。这是在一个小型生产环境中运行的,并且希望避免与现有数据搞混......(在我们现场使用之前自然会在 dev 上进行测试)。
任何想法/提示/解决方案将不胜感激。
先感谢您。
编辑
现在表单正确显示,我在上传多个图像时收到以下错误。
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"}