Reputation: 10211
I am trying to integrate stripe to receive payments/donation in my rails4 app. I am getting undefined method "stripe_card_token" for nil:NilClass
I am not understanding why this is happening.
At first I thought it was a problem with string parameters but it still won't work, not sure why. Did I do a typo that I can't spot? Any guidance appreciated.
charges controller
class ChargesController < ApplicationController
def new
@charge = Charge.new
end
def create
@charge = Charge.new(charges_params)
if @charge.save_with_payment
redirect_to @charge, :notice => "Thank you for subscribing!"
else
render :new
end
end
def charges_params
params.require(:charge).permit(:stripe_card_token)
end
end
Charges Model:
class Charge < ActiveRecord::Base
attr_accessor :stripe_card_token
def save_with_payment
if valid?
customer = Stripe::Customer.create(card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
end
My contest#show view:
<%= form_for @charge do |f| %>
<% if @charge.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(@charge.errors.count, "error") %> prohibited this charge from being saved: </h2>
<ul>
<% @charge.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :stripe_card_token %>
<%= f.hidden_field :contest_id %>
<%= f.hidden_field :user_id %>
<div class="field">
<%= f.label :amount %>
<%= f.text_field :amount %>
</div>
<% if @charges.stripe_card_token.present? %>
Credit card has been provided.
<% else %>
<div class="field">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
</div>
<% end %>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<% end %>
My charges schema table:
create_table "charges", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.string "stripe_customer_token"
t.integer "contest_id"
t.integer "user_id"
t.decimal "amount"
end
My contest controller show action:
def show
@contest = Contest.find(params[:id])
@charge = Charge.new
end
Upvotes: 0
Views: 646
Reputation: 10406
<% if @charges.stripe_card_token.present? %>
You called it @charge
in your controller, you've not defined @charges
Upvotes: 1