Rails - 缺少必需参数

时间:2014-05-27 11:53:11

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

我得到"缺少必需的参数:order_id"我的Rails4应用程序中的错误。我不知道" order_id"属性来自,因为我已经在订单模型中。我的代码出了什么问题?

完整错误=>

ArgumentError in OrdersController#create
Missing required parameter: order_id
Extracted source (around line #12):

11 def purchase
12 response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
13 transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
14 participation.update_attribute(:payment_status, true) if response.success?
15 response.success?

app/models/order.rb:12:in `purchase'
app/controllers/orders_controller.rb:36:in `block in create'
app/controllers/orders_controller.rb:34:in `create'

Request Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"gyRo+t8OpFhjZ6D2wQogp6pkYu7nanGbQJR1sm4k/YQ=",
 "order"=>{"participation_id"=>"5",
 "first_name"=>"DSAdsa",
 "last_name"=>"DSAdsa",
 "card_brand"=>"visa",
 "card_number"=>"4355084351084158",
 "card_verification"=>"000",
 "card_expires_on(3i)"=>"1",
 "card_expires_on(2i)"=>"5",
 "card_expires_on(1i)"=>"2018"},
 "commit"=>"Order Ekle"}

型号=>

class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :participation
  has_many :transactions, :class_name => "OrderTransaction"
  validate :validate_card, :on => :create

  attr_accessor :card_number, :card_verification

  def purchase
    response = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
    transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
    participation.update_attribute(:payment_status, true) if response.success?
    response.success?
  end

  def price_in_cents
    (participation.examination.exam_fees.first.fee*100).round
  end

  private

  def purchase_options
    {
      :ip => ip_address,
      :billing_address => {
        :BillToName => "Ryan Bates",
        .......
      }
    }
  end

  def validate_card
    unless credit_card.valid?
      credit_card.errors.full_messages.each do |message|
        errors[:base] << message
      end
    end
  end

  def credit_card
    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
      :brand              => card_brand,
      :number             => card_number,
      :verification_value => card_verification,
      :month              => card_expires_on.month,
      :year               => card_expires_on.year,
      :first_name         => first_name,
      :last_name          => last_name
    )
  end
end

控制器=&gt;

require 'digest/sha1'

class OrdersController < ApplicationController
  before_filter :authenticate_user!
  before_action :set_order, only: [:show, :edit, :update, :destroy]
  skip_before_action :verify_authenticity_token

  def index
    @orders = Order.all
  end

  def show
  end

  def new
    @order = Order.new
  end

  def edit
  end

  def create
    @order = Order.new(order_params)
    @order.ip_address = request.remote_ip
    @order.user_id = current_user.id

    respond_to do |format|
      if @order.save
        if @order.purchase
          format.html { render action: "success", notice: 'Sınav giriş belgesi başarıyla oluşturuldu!' }
        else
          format.html { render action: "failure" }
        end
      else
        format.html { render action: 'new' }
      end
    end
  end

  def update
    respond_to do |format|
      if @order.update(order_params)
        format.html { redirect_to @order, notice: 'Order was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @order.destroy
    respond_to do |format|
      format.html { redirect_to orders_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_order
      @order = Order.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
      params.require(:order).permit(:id, :ip_address, :first_name, :last_name, :card_brand, :card_expires_on, :user_id, :participation_id, :card_number, :card_verification)
    end
end

查看=&gt;

<%= simple_form_for(@order, html:{class: "well", multipart: true}, :method => :post) do |f| %>
      <%= f.input :participation_id, collection: Participation.where(user_id: current_user.id), as: :select, label_method: lambda{|x| x.examination.name}, label: 'Sınav Seçiniz' %>
      <%= f.input :first_name, label: 'Adınız' %>
      <%= f.input :last_name, label: 'Soyadınız' %>
      <%= f.input :card_brand, collection: [["Visa", "visa"], ["Master Card", "master"]], label: 'Kart Tipi' %>
      <%= f.input :card_number, label: 'Kart Numarası' %>
      <%= f.input :card_verification, label: 'CVV2' %>
      <%= f.input :card_expires_on, as: :date, label: 'Kart Son Kullanma Tarihi', discard_day: true, start_year: Date.today.year,
        end_year: Date.today.year + 10, order: [:month, :year] %>
    <%= f.button :submit %>
<% end %>

0 个答案:

没有答案
相关问题