Reputation: 39
I am using rails 4 and balanced.js and am trying to post the returned token to the back end using jQuery.post. I have the correct url getting passed but receive a 400 error. TO CLARIFY: I am getting an error on the jquery.ajax after receiving a valid token from balanced.
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)
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)
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
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
end
UPDATE: here is the full error code from the chrome debugger:
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)
UPDATE 2: here is the error from rails server
ActionController::ParameterMissing - param is missing or the value is empty
actionpack (4.1.4) lib/action_controller/metal/strong_parameters.rb:183:in `require'
Upvotes: 0
Views: 2244
Reputation: 282
Your data payload lacks the key "table_charge"
JS
data: {uri: fundingInstrument.href, event_id: urlid}
...
RAILS
def table_charge_params
params.require(:tablecharge).permit(:name, :email)
end
require is pretty descriptive here, it blows up if the key is absent
Upvotes: 1