I have a Rails app with a User.rb class and a Promember.rb class. Promember.rb belongs_to :user and User.rb has_one :promember.
In my Promember.rb model, I am using the following update_stripe
method (adapted from this tutorial https://tutorials.railsapps.org/tutorials/rails-stripe-membership-saas) to handle signup for promembership through Stripe. If there's no customer_id present (i.e. if it's a new customer),I am getting email and name information from the associated user instance (at least that's what I think I'm doing) when I call the Stripe::Customer.create method
:email => user.email,
:description => user.name,
That works fine for creating the new Stripe customer, but in situations when the customer_id is present (and I'm not creating a new customer), trying to get the name and email from the user like this
customer.description = user.name
customer.email = user.email
I'm getting this error
NoMethodError in PromembersController#create
undefined method `name' for nil:NilClass
Why would 'user' be nil in this situation?
def update_stripe
if customer_id.nil?
customer = Stripe::Customer.create(
:email => user.email,
:description => user.name,
:card => stripe_token,
:plan => '1'
)
else
customer = Stripe::Customer.retrieve(customer_id)
if stripe_token.present?
customer.card = stripe_token
end
customer.description = user.name
customer.email = user.email
customer.save
end
self.last_four_digits = customer.active_card.last4
self.customer_id = customer.id
self.stripe_token = nil
rescue Stripe::StripeError => e
logger.error "Stripe Error: " + e.message
errors.add :base, "#{e.message}."
self.stripe_token = nil
false
end
Update
This is the create action (using strong parameters in Rails 4 app)
def create
@promember = current_user.build_promember(promember_params)
if @promember.save
current_user.add_role :pro
redirect_to lawyer_profile_path(current_user), :notice => "award created question. It will be reviewed if you checked box"
else
redirect_to (:back), :notice => "There was an error."
end
end
private
def promember_params
params.require(:promember).permit(:pro, :customer_id, :user_id, :last_four_digits, :stripe_token, :stripe_card_token)
end
Update
These are the params getting passed to the create method
Parameters: {"utf8"=>"✓", "authenticity_token"=>"3zq3tFwp1AgR3yBWGenW45kLqUzVlg96dP8Haa4bVKQ=", "promember"=>{"stripe_token"=>""}, "commit"=>"Create Promember"}
{"utf8"=>"✓", "authenticity_token"=>"3zq3tFwp1AgR3yBWGenW45kLqUzVlg96dP8Haa4bVKQ=", "promember"=>{"stripe_token"=>""}, "commit"=>"Create Promember", "action"=>"create", "controller"=>"promembers"}
promemberparams
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 22 ORDER BY "users"."id" ASC LIMIT 1
Promember Load (31.4ms) SELECT "promembers".* FROM "promembers" WHERE "promembers"."user_id" = $1 ORDER BY "promembers"."id" ASC LIMIT 1 [["user_id", 22]]
(0.2ms) BEGIN
(0.3ms) ROLLBACK
Completed 500 Internal Server Error in 3586ms
NoMethodError (undefined method `name' for nil:NilClass):
app/models/promember.rb:38:in `update_stripe'
app/controllers/promembers_controller.rb:16:in `create'