Getting 'recipient.id' Error

I built the Stripe integration into my app following your Etsy demo. It was successful and I was able to make purchases. Now I am trying to sign up as a new vendor to test. I was added successfully and I added the first listing information which included the pay to bank account info which I grabbed the test routing info from Stripe's website. All good. The listing shows up fine. Then I went to add a second listing, I filled out the new listing form, the bank pay to form now not showing because I already added the routing information, but when I click to save, I'm now getting a 'recipient.id' error in my ListingsController#create: ERROR MESSAGE IN BROWSER: NoMethodError in ListingsController#create undefined method `id' for nil:NilClass Extracted source (around line #93): ---------- end current_user.recipient = recipient.id current_user.save respond_to do |format| ----------- HERE'S WHAT I HAVE IN MY LISTINGS CONTROLLER: def create @collections = Collection.where(user: current_user) @listing = Listing.new(listing_params) @listing.user_id = current_user.id @user = current_user if current_user.recipient.blank? Stripe.api_key = ENV["STRIPE_API_KEY"] token = params[:stripeToken] recipient = Stripe::Recipient.create( :name => current_user.name, :type => "individual", :bank_account => token ) end current_user.recipient = recipient.id current_user.save respond_to do |format| if @listing.save format.html { redirect_to seller_url } format.json { render :show, status: :created, location: @listing } else format.html { render :new } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end Any ideas to try? Thanks so much. P.S. Sorry I didn't know which lesson discussion page to add this question to, so I opted to ask the question here.

Posted over 4 years ago by Amy Peterson
Posted over 4 years ago by Amy Peterson

Oh thank goodness! I was hoping it was something simple and user error. Thanks very much Alex!

1
Posted over 4 years ago by Alex Yang

It looks like you have a typo: 'end' should be two lines lower :-)

1
Posted over 4 years ago by Amy Peterson

def create
@collections = Collection.where(user: current_user)
@listing = Listing.new(listing_params)
@listing.user_id = current_user.id
@user = current_user

if current_user.recipient.blank?
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]

recipient = Stripe::Recipient.create(
:name => current_user.name,
:type => "individual",
:bank_account => token
)
end

current_user.recipient = recipient.id
current_user.save

respond_to do |format|
if @listing.save
format.html { redirect_to seller_url }
format.json { render :show, status: :created, location: @listing }
else
format.html { render :new }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
end

1