0

我有一个非常简单的应用程序,其流程如下所示:

用户阅读一些副本,决定他们要购买产品并填写表格,请参阅确认页面。

我的控制器出了点问题,但我无法确定我在哪里得到了错误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

我不确定事情在哪里搞砸了,经过大量谷歌搜索,我转向你们。帮助将是巨大的。

编辑:

咨询 Rails 控制台表明我的应用程序由于某种原因没有创建新的客户记录。但是,充电工作正常。未创建的客户必须是这方面的先驱。

编辑 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"=>"yourname@example.com"}, "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)
4

3 回答 3

1

如果用户直接执行receipt操作,则session[:customer_id]可能为零。这就是你得到错误的原因。如果发出create(可能)POST 请求,则不会发生这种情况。在这种情况下,会话变量在重定向到receipt操作之前已被填充。

于 2012-08-28T22:59:57.577 回答
0

这一行:

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

抛出错误,因为 session[:customer_id] 为 nil

您可能希望使用不会抛出异常但会返回 nil 记录的 find_by_id。

然后你将不得不处理@customer 为零的情况。

于 2012-08-28T22:51:35.807 回答
0

在 UserController 中添加这个

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

而不是把“user_id”放在 session[:user_id]
它对我有用

于 2013-07-01T18:11:31.030 回答