【问题标题】:How to upload multiple files without nested form using Rails 4?如何使用 Rails 4 上传多个没有嵌套表单的文件?
【发布时间】:2015-11-21 12:31:44
【问题描述】:

谁能告诉我如何使用carrierwave实现多个文件上传而不需要单独的文件模型?

我的模型是expense_details.rb,我需要为这些费用上传多张收据。

【问题讨论】:

    标签: ruby-on-rails-4 carrierwave


    【解决方案1】:

    你需要:

    1) 多态附件资源

    2) rails 上传 gem(https://github.com/carrierwaveuploader/carrierwave)

    3) 一个js多重上传器(https://blueimp.github.io/jQuery-File-Upload/)

    4) 易于使用的 2 & 3(github.com/tors/jquery-fileupload-rails)

    分贝:

    class CreateAttachments < ActiveRecord::Migration
      def change
        create_table :attachments do |t|
          t.string :name
          t.string :link
          t.integer :attachmentable_id
          t.string :attachmentable_type
          t.integer :user_id
    
          t.timestamps
        end
    
        add_index :attachments, :user_id
        add_index :attachments, [:attachmentable_id, :attachmentable_type]
      end
    end
    

    控制器:

    class AttachmentsController < ApplicationController
      ...
      def create
        @attachment = Attachment.new(params[:attachment])
        @attachment.name = params[:attachment][:link].original_filename
    
        if @product_attachment.save
          # do something
        else
          # do something
        end
    end
    

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多