使用设置值更新某个列

时间:2016-06-24 11:17:51

标签: ruby-on-rails forms postgresql methods

我有一组客户在我的商店中有信用,我正在尝试找到他们当前的余额,然后用新的余额进行调整,但是这样做的时候我一直无法获得金额无效,但我无法继续工作。为什么这是

CONTROLLER

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 PARTIAL

%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)

1 个答案:

答案 0 :(得分:0)

问题在于您的update操作

您将错误的值传递给amount Braintree::Transaction :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 的实际数量customer.store_credit.add_credit(:amount_added)发生此错误的原因。它表示无效的金额类型。所以将您的行动更新为

customer.store_credit.add_credit params[:amount_to_add]

P.S 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

<强>更新

$container = $layout->getContainer('name'); $container->setAttribute('htmlClass', 'class'); 方法更改为

<picture>
   <img class="post__featured-image" data-lazy="@(Configuration.URLs.MediaPath + ad.SmallImagePath)" />
</picture>