【问题标题】:update a certain column with a set value用设定值更新某个列
【发布时间】:2016-06-24 11:17:51
【问题描述】:

我有一组客户在我的商店中有积分,我试图找到他们当前的余额,然后用新的余额进行调整,但是这样做时我不断收到 Amount Invalid 但我无法在工作期间为什么会这样

控制器

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

形成部分

%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

标签: ruby-on-rails forms postgresql methods


【解决方案1】:

问题在于您的update 操作

您向Braintree::Transaction 中的amount 字段传递了错误的值,您传递的是:amount_added(这是一个符号)而不是params[:amount_to_add] 的实际金额,这就是发生此错误的原因。它说无效的金额类型。所以将您的操作更新为

def update
    result = Braintree::Transaction.sale(
        :amount => params[:amount_to_add].to_f,
        :customer_id => customer.customer_cim_id,
        :options => {
            :submit_for_settlement => true
        }
    )
    if result.success?
      p 'Transaction Successful'
      customer.store_credit.add_credit(params[:amount_to_add].to_f)
      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

P.S 同样是customer.store_credit.add_credit(:amount_added) 改成customer.store_credit.add_credit params[:amount_to_add]

更新

将add_credit方法改为

  def remove_credit(amount_to_remove)
    credit_amount = self.amount - amount_to_remove.to_f.round(2)
    self.update(amount: credit_amount)
  end

【讨论】:

  • 这并没有让它成为浮动
  • 修复了一个错误,但仍然无法保存到数据库
  • 引入了无法转换为_f的新错误
  • 实际上它确实修复了它,我错过了添加信用的参数
  • 哦,明白了,顺便说一句,我在答案中明确写了将正确的参数传递给 add_credit 。我很乐意提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多