为什么我无法从自定义控制器中插入模型中的数据?

时间:2015-01-14 22:57:12

标签: ruby-on-rails ruby ruby-on-rails-4 payment

我为名为“Charge”的模型创建了一个脚手架,但我想从我创建的“Checkout”自定义控制器中将数据插入到模型中。

我有以下代码

##routes.rb
get "checkout/pay", to: 'checkout#index'
post "checkout/charge"

## app/controllers/checkout_controler.rb
require "conekta"
Conekta.api_key = "xxxxxxxxxxxxxxxx"

# GET 'checkout/pay'
def index
  redirect_to root_path, notice: "your cart is empty" if @cart.line_items.empty?
end

# POST 'checkout/charge'
def charge
  redirect_to root_path, notice: "your cart is empty" if @cart.line_items.empty?

  total_price = @cart.total_price_cart * 100

  begin 
    charge = Conekta::Charge.create({
      amount: total_pricec.to_i,
      currency: "MXN",
      description: "Pizza Delivery",
      reference_id: "sku product",
      card: params[:conektaTokenId]
    })
    #Conekta::Charge.create() runs successfully and return a hash of values

    @charge = Charge.new(id: charge.id, amount: charge.amount, livemode: charge.livemode, created_at: charge.created_at, status: charge.status, currency: charge.currency, description: charge.description, reference_id: charge.reference_id, failure_code: charge.failure_code, failure_message: charge.failure_message)
    if @charge.save
      redirect_to products_path, notice: "charge was created"
    else
      redirect_to root_path
    end
  rescue Conekta::Error 
    puts e.message 
  end
app / views / checkout / index.html.erb

中的

<%= form_tag('checkout/charge, id:'card-form') do %>
    fields .....
    <button>Charge</button>
<% end %>

更新 我可以达到方法charge = Conekta :: Charge.create(),并且它成功运行创建付款,但该方法无法在“Charge”模型中插入数据并保存数据。 当我去'/ charge'时它是空的。

0 个答案:

没有答案