0

I've got a very simple app where the flow looks like this:

User reads some copy, decides they want to purchase the product and fill out the form, see confirmation page.

Something is wrong in my controller but I can't quite pinpoint it where I'm getting the error Couldn't find Customer without an ID.

class CustomersController < ApplicationController
#  before_filter :load_customer, :only => :show

  def new
    @customer = Customer.new
  end

  def create
    @customer = Customer.new(params[:customer])

    if @customer.save
      session[:customer_id] = @customer.id
      purchase
    else
      flash[:error] = "Please enter a valid email address"
      redirect_to :signup
    end
  end

  def signup
    @customer = Customer.new
  end


  def purchase
    Stripe.api_key = STRIPE['secret']
    charge = Stripe::Charge.create(
    :amount => 2000,
    :currency => "usd",
    :card => params[:stripe_token],
    :description => "My Product Name"
    )
    redirect_to receipt_path
  end

  def receipt
    @customer = Customer.find(session[:customer_id])

    @name = @customer.name
    @email = @customer.email
    @product = @customer.product
  end

  # private
  # 
  # def load_customer
  #   @customer = Customer.find(session[:customer_id])
  #   redirect_to request.path.gsub(params[:id], session[:customer_id].to_s) if params[:id] != session[:customer_id].to_s
  # end

end

I'm not sure where things are screwing up and after much Googling, I'm turning to you guys. Help would be huge.

EDIT:

Consulting with Rails console shows that my application isn't making new customer records for some reason. Charging is working, however. Customers not being created must be a precursor to this.

EDIT 2: Development.log

Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2012-08-28 15:58:11 -0700
Served asset /bootstrap.js - 304 Not Modified (0ms)
[2012-08-28 15:58:11] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started POST "/checkout" for 127.0.0.1 at 2012-08-28 15:58:12 -0700
Processing by CustomersController#purchase as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"k2aW/CAkNfwDSMHHvzbuOwm+Xua0qb2LJ4LtrtRvyvk=", "customer"=>{"name"=>"Your name", "email"=>"[email protected]"}, "stripe_token"=>"tok_0GUvwKPwo6jfEu"}
Redirected to http://localhost:3000/receipt
Completed 302 Found in 1064ms (ActiveRecord: 0.0ms)


Started GET "/receipt" for 127.0.0.1 at 2012-08-28 15:58:14 -0700
Processing by CustomersController#receipt as HTML
Completed 500 Internal Server Error in 0ms

ActiveRecord::RecordNotFound (Couldn't find Customer without an ID):
  app/controllers/customers_controller.rb:38:in `receipt'


  Rendered /Users/zack/.rvm/gems/ruby-1.9.3-p194@beat-the-herd/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.9ms)
  Rendered /Users/zack/.rvm/gems/ruby-1.9.3-p194@beat-the-herd/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (5.6ms)
  Rendered /Users/zack/.rvm/gems/ruby-1.9.3-p194@beat-the-herd/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (23.7ms)
1
  • Could you please submit the development log in full from the moment you go to signup to the moment you call purchase? That would be helpful Aug 28, 2012 at 22:57

3 Answers 3

1

If the user routes directly to receipt action then session[:customer_id] may be nil. That's why you get the error. This is not happened if a create (possibly) POST request is issued. In this case, the session variable has been populated before the redirection to the receipt action.

5
  • This is exactly what's happening I think. Since my form starts <%= form_for @customer, :url => purchase_path, :method => :post do |f| %> Aug 28, 2012 at 23:03
  • How would you recommend that I remedy this so that the user is created, then charged, then shown their receipt? Aug 28, 2012 at 23:05
  • The problem is that you POST to purchase_path where the session[:customer_id] = @customer.id is not executed :)!
    – alup
    Aug 28, 2012 at 23:05
  • How would you recommend that I fix this? Aug 28, 2012 at 23:08
  • I believe you should separate the logic of creation and purchase things. Think all the possible state transitions and then implement the actions.
    – alup
    Aug 28, 2012 at 23:09
0

This line :

@customer = Customer.find(session[:customer_id])

throws an error because session[:customer_id] is nil

You might want to use find_by_id that won't throw an Exception but will return an nil record.

Then you'll have to deal with the case of @customer being nil.

4
  • When I look in my rails console, it looks like the customer isn't actually being created which is what might be causing this. Any idea why? Aug 28, 2012 at 22:53
  • @customer.save returns true ?
    – Intrepidd
    Aug 28, 2012 at 22:55
  • @customer.save is returning true, I believe, because the purchase method is being called successfully Aug 28, 2012 at 22:59
  • try to log the value of session[:customer_id] after setting it. Maybe your browser does not accept cookies so the session is not set when going to the receipt action ?
    – Intrepidd
    Aug 28, 2012 at 23:00
0

Add this in UserController

def show <br>
@user = User.find(session[:user_id])<br>
@profile = @user.profile<br>
end

Rather than putting "user_id" put session[:user_id]
It worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.