Rails Strong_params passing "param is missing"

396 Views Asked by At

I have a rails 4.1 app that is using Balanced to process credit cards. The credit card form I have is using ajax to post to the backend but I am getting the following error:

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

here is my controller:

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

here is my model:

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

here is my 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)

here is my view:

<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

There are 1 best solutions below

5
kittyminky On

The first value in your data object has to be :tablecharge as you specified that all the params belong to that object.

So the data you are passing in your ajax request should be formatted like this:

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