Rails Strong_params传递“param is missing”

时间:2014-08-07 23:50:46

标签: ruby-on-rails ruby-on-rails-4 balanced-payments

我有一个使用Balanced来处理信用卡的rails 4.1应用程序。我的信用卡表单是使用ajax发布到后端但我收到以下错误:

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

这是我的控制器:

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)
    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(:uri, event_attributes: :id)
end

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
attr_accessor :uri
end

这是我的Javascript:

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 "fuck"})

        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)

这是我的观点:

<div class="authform">
<%= javascript_tag do %>
window.urlid = "<%= @table.event_id %>"
<% end %>
<%= simple_form_for [@event, @table] do |f| %>
    <%= f.input :cc_name, input_html: {id: 'cc-name'}%>
    <%= f.input :cc_number, input_html: {id: 'cc-number'} %>
    <%= f.input :cc_expiration_month, as: :integer, input_html: {id: 'cc-expiration-month'} %>
    <%= f.input :cc_expiration_year, as: :integer, input_html: {id: 'cc-expiration-year'} %>
    <%= f.input :cc_ccv, input_html: {id: 'cc-ccv'} %>
    <%= f.input :uri, as: :hidden %>
    <%= f.button :submit, id: 'cc-submit', remote: true  %>
<% end %>

1 个答案:

答案 0 :(得分:1)

data对象中的第一个值必须为:tablecharge,因为您指定所有params属于 对象。

因此,您data中传递的ajax request应格式如下:

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