1

试图在不破坏现有数据保存结构的情况下实现这一点。我有一个将单个图像上传到资产的表单。编辑修改了 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"}
4

1 回答 1

1

考虑到您将记录保存到与表单相同的数据库中,您似乎需要使用 打折accepts_nested_attributes_for,这是我通常与 Paperclip 一起使用的

一次保存多条记录

看来您的主要问题是一次保存多条记录:

No handler found for {"0"=>

这表明 Rails 无法计算 save 函数,因为它只是为单个参数设计的。您实际上必须使用手动 SQL 查询来保存记录,例如这些资源:

本质上,您必须以某种方式遍历您的 params 数组,并以一种批量方式保存数据。这是您可以做到的一种方法:

根据这些链接之一,您可以使用activerecord-importgem

def create
assets = []
    params[:asset].each do |k,v| 
       assets << Asset.new(params[:asset][k])
    end
    Asset.import assets
end
于 2013-10-22T08:54:37.290 回答