jQuery.post返回400错误请求错误[rails]

时间:2014-08-05 01:34:20

标签: javascript jquery ruby-on-rails ajax balanced-payments

我正在使用rails 4和balanced.js,并尝试使用jQuery.post将返回的令牌发布到后端。我有正确的URL传递但收到400错误。 TO CLARIFY:从平衡点收到有效令牌后,我在jquery.ajax上收到错误。

jQuery ->
$('#cc-submit').click (e) ->
    e.preventDefault()

    handleResponse = (response) ->
        if (response.status_code == 201)

            fundingInstrument = (if response.cards? then response.cards[0] else response.bank_accounts[0])

            alert(fundingInstrument.href)

            $('#tablecharge_uri').val(fundingInstrument.href)

            url = '/events/' + urlid + '/tablecharges'

            alert(url)

            jQuery.ajax({type: "POST", url: url, data: {uri: fundingInstrument.href, event_id: urlid}, sucess: (data) ->
                alert data.id
            error: (data) ->
                alert "not cool"})

        else
            alert(response.status + JSON.stringify(response, false, 1))

    payload = {
    name: $('#cc-name').val()
    number: $('#cc-number').val()
    expiration_month: $('#cc-expiration-month').val()
    expiration_year: $('#cc-expiration-year').val()
    ccv: $('#cc-ccv').val()
    }

    balanced.card.create(payload, handleResponse)

这是我的控制器:

class TablechargesController < ApplicationController
def new
    @event = Event.find(params[:event_id])
    @table = @event.tablecharges.build
end

def index
    @event = Event.find(params[:event_id])
    @table = @event.tablecharges.all
end

def create
    @event = Event.find(params[:event_id])
    @table = @event.tablecharges.build(table_charge_params)
    respond_to do |format|
        format.js { render json: 'Your message', status: :ok } 
    end
    if @table.save
        redirect_to @event, :notice => "Thanks for the cash"
    else
        render :new
    end
end

private

def table_charge_params
    params.require(:tablecharge).permit(:name, :email)
end

这是我的模特:

class Tablecharge < ActiveRecord::Base
belongs_to :event
attr_accessor :cc_name
attr_accessor :cc_number
attr_accessor :cc_expiration_month
attr_accessor :cc_expiration_year
attr_accessor :cc_ccv
end

更新: 这是chrome调试器的完整错误代码:

POST "/events/7/tablecharges" 400 (Bad Request) 
jquery.js?body=1:9632
send jquery.js?body=1:9632
jQuery.extend.ajax jquery.js?body=1:9177
handleResponse tablecharges.js?body=1:13
ret balanced.js:433
window.(anonymous function) balanced.js:395
(anonymous function)

更新2: 这是来自rails服务器的错误

 ActionController::ParameterMissing - param is missing or the value is empty
 actionpack (4.1.4) lib/action_controller/metal/strong_parameters.rb:183:in `require'

1 个答案:

答案 0 :(得分:1)

您的数据有效负载缺少密钥&#34; table_charge&#34;

JS

data: {uri: fundingInstrument.href, event_id: urlid}
...

钢轨

def table_charge_params
    params.require(:tablecharge).permit(:name, :email)
end

需要在这里非常具有描述性,如果密钥不存在则会爆炸