我有一組有我的商店內信貸客戶,我試圖找到自己的當前餘額,然後用一個新的平衡調整,但這樣做我不斷收到的無效時但我不能爲生活工作了爲什麼這是更新與設定值某一列
控制器
class Admin::UserDatas::StoreCreditsController < Admin::UserDatas::BaseController
helper_method :customer
def show
@store_credit = customer.store_credit
end
def edit
form_info
end
def update
result = Braintree::Transaction.sale(
:amount => :amount_added,
:customer_id => customer.customer_cim_id,
:options => {
:submit_for_settlement => true
}
)
if result.success?
p 'Transaction Successful'
customer.store_credit.add_credit(:amount_added)
redirect_to admin_user_datas_user_store_credits_url(customer), :notice => "Successfully updated store credit."
else
result.errors.each do |error|
puts error.message
customer.errors.add(:base, error.message)
form_info
render :edit, :notice => error.message
end
end
end
private
def form_info
end
def customer
@customer ||= User.includes(:store_credit).find(params[:user_id])
end
def amount_added
params[:amount_to_add].to_f
end
end
方法
def remove_credit(amount_to_remove)
credit_amount = "#{self.amount} - #{amount_to_remove.to_f.round(2)}"
self.update(amount: credit_amount)
end
def add_credit(amount_to_add)
credit_amount = "#{self.amount} + #{amount_to_add.to_f.round(2)}"
self.update(amount: credit_amount)
end
FORM局部
個%ul.admin-standard_form.prepend-8{:id => ""}
%li.field
= label :amount_to_add, 'Amount to Add'
= text_field_tag :amount_to_add
編輯模板
%h3
Change #{customer.name} Store Credit
%ul
%li
%label Current Amount:
= number_to_currency customer.store_credit_amount
= form_for customer, :url => admin_user_datas_user_store_credits_path(customer) do |f|
= render :partial => 'form', :locals => {:f => f}
.actions= f.submit 'Update' , :class => 'button'
錯誤
Started PATCH "/en/admin/user_datas/users/1/store_credits" for 127.0.0.1 at 2016-06-24 21:56:08 +1000
Processing by Admin::UserDatas::StoreCreditsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "amount_to_add"=>"200", "commit"=>"Update", "locale"=>"en", "user_id"=>"1"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["remember_token", "cfe807bd1caa3763df52dcb18de8c9d4f350b933"]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
StoreCredit Load (0.5ms) SELECT "store_credits".* FROM "store_credits" WHERE "store_credits"."user_id" IN (1)
I, [2016-06-24T21:56:09.418606 #8775] INFO -- : [Braintree] [24/Jun/2016 11:56:09 UTC] POST /merchants/XXXX/transactions 422
Amount is an invalid format.
Rendered admin/user_datas/store_credits/_form.html.haml (0.7ms)
Rendered admin/user_datas/store_credits/edit.html.haml within layouts/superman (4.0ms)
Rendered shared/_meta_data.html.haml (4.6ms)
Rendered admin/shared/_side_menu.html.haml (2.8ms)
Completed 200 OK in 1085ms (Views: 47.2ms | ActiveRecord: 1.3ms)
更多的錯誤
Started PATCH "/en/admin/user_datas/users/1/store_credits" for 127.0.0.1 at 2016-06-24 22:20:46 +1000
Processing by Admin::UserDatas::StoreCreditsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "amount_to_add"=>"20", "commit"=>"Update", "locale"=>"en", "user_id"=>"1"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["remember_token", "cfe807bd1caa3763df52dcb18de8c9d4f350b933"]]
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
StoreCredit Load (0.5ms) SELECT "store_credits".* FROM "store_credits" WHERE "store_credits"."user_id" IN (1)
I, [2016-06-24T22:20:50.681897 #8775] INFO -- : [Braintree] [24/Jun/2016 12:20:50 UTC] POST /merchants/mbmb637xwpzgxbrd/transactions 201
"Transaction Successful"
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Redirected to http://0.0.0.0:3000/en
Completed 302 Found in 3848ms (ActiveRecord: 1.6ms)
您可以複製錯誤記錄在這裏。它會縮小你面對 –
添加日誌@QaisarNadeem –