如何使用paperclip,angular js,ng-file-upload在rails中制作可选附件

时间:2016-02-17 03:58:39

标签: javascript ruby-on-rails angularjs

我有"付款"具有文件附件的表单,用于上传receipt_img,我使用paperclip,angular.js作为前端和ng-file-upload插件。

如果附件已填满,一切正常,

但我想将此附件作为可选附件, 我在这个stackoverflow中的另一个问题中尝试了很多方法

same-question-1same-question-2

但没有人在我的表单上工作,总是显示错误

No handler found for null

Completed 422 Unprocessable Entity in 119ms (Views: 15.4ms | ActiveRecord: 41.7ms)

我想问的是,

在ng-file-upload插件中是否有任何方法可以使附件变得有用,所以如果安全,它就不会起作用?

有什么方法可以解决它吗?

这是我的回形针回形针:

has_attached_file :receipt_img, dependent: :destroy
validates_attachment :receipt_img, :content_type => { :content_type => /\Aimage\/.*\Z/ }, :size => { :in => 0..2.megabytes }

这是我的payment.coffee:

Sprangular.service "Payment", ($http, $q, _, Env, Account, Cart, Flash, Upload) ->



service =
    confirmPaymentBankTransfer: (order, payment, bankTransfer) ->

  url = Spree.mountedAt() + "/api/orders/#{order.number}/payments/#{payment.id}"

  bankTransfer.receipt_img.upload = Upload.upload
    url: url
    method: 'PUT'
    headers:
      'X-Spree-Order-Token': order.token
    data:
      payment:
        receipt_img: bankTransfer.receipt_img
        bank_name: bankTransfer.bank_name
        deposited_on: bankTransfer.deposited_on
        account_name: bankTransfer.account_name
        account_no: bankTransfer.account_no
        transaction_reference_no: bankTransfer.transaction_reference_no
        our_bank_name: bankTransfer.our_bank_name

  # $http.put(url, $.param(params), config)
  #   .success (response) ->
  #     Flash.success 'app.account_updated'
  #   .error (response) ->
  #     Flash.error 'app.account_update_failed'
service

感谢,

编辑添加controller.coffee

'use strict'

class Sprangular.BankTransfer
  Validity.define @,
    deposited_on: 'required'
    bank_name: 'required'
    account_no: 'required'
    transaction_reference_no: 'required'
    our_bank_name: 'required'

  constructor: (deposited_on=null, bank_name=null, account_name=null, account_no=null, transaction_reference_no=null, our_bank_name=null, receipt_img=null) ->
    @deposited_on = deposited_on
    @bank_name = bank_name
    @account_name = account_name
    @account_no = account_no
    @transaction_reference_no = transaction_reference_no
    @our_bank_name = our_bank_name
    @receipt_img = receipt_img

  init: ->
    @id = @id
    @deposited_on = @deposited_on
    @bank_name = @bank_name
    @account_name = @account_name
    @account_no = @account_no
    @transaction_reference_no = @transaction_reference_no
    @our_bank_name = @our_bank_name
    @receipt_img = @receipt_img

  same: (other) ->
    @id == other.id

2 个答案:

答案 0 :(得分:0)

validates_attachment要求附件存在并符合您声明的所有规格。如果您希望附件是可选的,请使用allow_nil

validates_attachment :receipt_img,
  :content_type => { :content_type => /\Aimage\/.*\Z/ },
  :size => { :in => 0..2.megabytes },
  :allow_nil => true

答案 1 :(得分:0)

哦,Alhamdulillah上帝, 经过一天的努力,我自己解决了这个问题。

在尝试了许多参考方法后,但没有人工作, 我通过在我的服务(payment.coffee)中创建条件来解决它,所以服务看起来像这样:

Sprangular.service "Payment", ($http, $q, _, Env, Account, Cart, Flash, Upload) ->

  service =

confirmPaymentBankTransfer: (order, payment, bankTransfer) ->
  if bankTransfer.receipt_img is null
    params =
      payment:
        bank_name: bankTransfer.bank_name
        deposited_on: bankTransfer.deposited_on
        account_name: bankTransfer.account_name
        account_no: bankTransfer.account_no
        transaction_reference_no: bankTransfer.transaction_reference_no

    config =
      ignoreLoadingIndicator: true
      headers:
        'X-Spree-Order-Token': order.token

    url = Spree.mountedAt() + "/api/orders/#{order.number}/payments/#{payment.id}"

    $http.put(url, $.param(params), config)
      .success (response) ->
        Flash.success 'app.account_updated'
      .error (response) ->
        Flash.error 'app.account_update_failed'

  else
    url = Spree.mountedAt() + "/api/orders/#{order.number}/payments/#{payment.id}"

    bankTransfer.receipt_img.upload = Upload.upload
      url: url
      method: 'PUT'
      headers:
        'X-Spree-Order-Token': order.token
      data:
        payment:
          receipt_img: bankTransfer.receipt_img
          bank_name: bankTransfer.bank_name
          deposited_on: bankTransfer.deposited_on
          account_name: bankTransfer.account_name
          account_no: bankTransfer.account_no
          transaction_reference_no: bankTransfer.transaction_reference_no
          our_bank_name: bankTransfer.our_bank_name  

    # $http.put(url, $.param(params), config)
    #   .success (response) ->
    #     Flash.success 'app.account_updated'
    #   .error (response) ->
    #     Flash.error 'app.account_update_failed'

  service

所以我把if else条件放在函数上。 它看起来有点脏,但它对我来说很好..

请求帮助..