轨道Strong_params通过"param is missing"



我有一个rails 4.1应用程序,它使用Balanced来处理信用卡。信用卡的形式,我有使用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 %>

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

所以你在ajax request中传递的data应该像这样格式化:

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

最新更新