I have a "payment" form that has a file attachment for upload the receipt_img, i'm using paperclip, angular.js in frontend and ng-file-upload plugin..
everything works fine if the attachment is filled,
but i want to make this attachment as an optional attachment, i have tried many ways in the other same question in this stackoverflow
same-question-1 , same-question-2
but no one works on my form, there's always show the error
No handler found for null
Completed 422 Unprocessable Entity in 119ms (Views: 15.4ms | ActiveRecord: 41.7ms)
the things i want to ask are,
is there any method in ng-file-upload plugin that make an attachment is ruquired, so it wouldn't work if empety?
is there any way how to solve it ?
here's my paperclip for receipt image :
has_attached_file :receipt_img, dependent: :destroy
validates_attachment :receipt_img, :content_type => { :content_type => /\Aimage\/.*\Z/ }, :size => { :in => 0..2.megabytes }
and it's my 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
thanks,
edit add 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
validates_attachment
is requiring that the attachment be present and meet all the specifications you declare. If you want the attachment to be optional, use allow_nil
:
validates_attachment :receipt_img,
:content_type => { :content_type => /\Aimage\/.*\Z/ },
:size => { :in => 0..2.megabytes },
:allow_nil => true
oh Alhamdulillah god, it's solved by myself after headche for a day..
after tried many ways of references, but no one worked at all, i'm solved it by creating a condition in my service (payment.coffee), so the service look like this :
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
so i put the if else condition on the function,. it's look a little dirty, but it's work fine for me..
thx for help..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.