gpt4 book ai didi

ruby-on-rails - CartsController 中的 NoMethodError#destroy - 使用 Rails 4 进行敏捷 Web 开发

转载 作者:行者123 更新时间:2023-12-04 05:58:02 25 4
gpt4 key购买 nike

我正在按照《使用 Rails 4 进行敏捷 Web 开发》一书学习 Rails,但我遇到了这个错误:

NoMethodError in CartsController#destroy
undefined method `name' for nil:NilClass

这是 Cart Controller 的 Destroy Action 。错误信息引用了行 @cart.destroy if @cart.id == session[:cart_id] :

# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url,
notice: 'Your cart is currently empty'}
format.json { head :no_content }
end
end

此操作是从 views/carts/shown.html.erb 中的“空车”按钮调用的

<%= button_to 'Empty cart', @cart, method: :delete, data: {confirm: 'Are you sure?'} %>

一切正常,但在创建添加列 price 的迁移之后到 table LineItems并修改方法 add_product来自 Carts为了处理这个新列,销毁操作不再有效。

这是 models/cart.rb

class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy

def add_product(product_id, product_price)
current_item = line_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id, price: product_price)
end
current_item
end

def total_price
line_items.to_a.sum { |item| item.total_price }
end

end

这是 models/line_item.rb :

class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart

def total_price
price * quantity
end
end

这就是 Action create来自 line_items_controller用于将产品添加到购物车:

before_action :set_cart, only: [:create]

...

# POST /line_items
# POST /line_items.json
def create
session[:counter] = 0
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id, product.price)

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

在此操作之前,方法 set_cart来自模块 CurrentCart被称为:

def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end

当我运行 Controller 测试时,没有出现任何故障。这是对操作 destroy 的测试来自购物车:

test "should destroy cart" do
assert_difference('Cart.count', -1) do
session[:cart_id] = @cart.id
delete :destroy, id: @cart
end

assert_redirected_to store_path
end

我看不出这个错误的原因,我需要你的帮助。

编辑

这是服务器的输出:

Started DELETE "/carts/15" for 127.0.0.1 at 2015-02-16 12:23:43 -0500
Processing by CartsController#destroy as HTML
Parameters: {"authenticity_token"=>"hLsBQ0fR5b9M2lfxMc5McU1+dqtnZ2OKABRZV79vdHo=", "id"=>"15"}
Cart Load (0.2ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", "15"]]
(0.2ms) begin transaction
LineItem Load (0.9ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? [["cart_id", 15]]
SQL (0.5ms) DELETE FROM "line_items" WHERE "line_items"."id" = ? [["id", 91]]
(0.2ms) rollback transaction
Completed 500 Internal Server Error in 73ms

NoMethodError (undefined method `name' for nil:NilClass):
app/controllers/carts_controller.rb:58:in `destroy'

谢谢:)

最佳答案

从 Rails 控制台尝试并得到这个:

2.2.0 :001 > carro = Cart.find(25)
Cart Load (12.9ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1 [["id", 25]]
=> #<Cart id: 25, created_at: "2015-02-17 01:10:49", updated_at: "2015-02-17 01:10:49">
2.2.0 :002 > carro.destroy
(0.2ms) begin transaction
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:75: warning: circular argument reference - reflection
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:79: warning: circular argument reference - reflection
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:83: warning: circular argument reference - reflection
/home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:102: warning: circular argument reference - reflection
LineItem Load (0.2ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? [["cart_id", 25]]
SQL (0.3ms) DELETE FROM "line_items" WHERE "line_items"."id" = ? [["id", 116]]
SQL (0.1ms) DELETE FROM "line_items" WHERE "line_items"."id" = ? [["id", 117]]
SQL (0.1ms) DELETE FROM "line_items" WHERE "line_items"."id" = ? [["id", 118]]
(0.2ms) rollback transaction
NoMethodError: undefined method `name' for nil:NilClass
from /home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:80:in `cached_counter_attribute_name'

发生的事情是 /home/xx/.rvm/gems/ruby-2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:75: warning: circular argument引用 - 反射 是由于 activerecord 中的错误引起的 GitHub issue .

我所做的是更新 Rails 版本。我使用的是 rails 4.0.0,更新到 rails 4.1.2 后问题就解决了。

关于ruby-on-rails - CartsController 中的 NoMethodError#destroy - 使用 Rails 4 进行敏捷 Web 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28545975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com