我在使用Rails 4进行敏捷Web开发之后学习Rails,我遇到了这个错误:
NoMethodError in CartsController#destroy
undefined method `name' for nil:NilClass
这是Cart控制器的动作Destroy。错误信息引用了行@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
添加到表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
这是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
当我运行控制器测试时,没有失败。这是对Cart中的操作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'
谢谢:)
答案 0 :(得分:10)
从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 reference - reflection
根据此GitHub issue activerecord
中的错误导致rails 4.0.0
。
我所做的是更新rails版本。我正在使用rails 4.1.2
,在更新到{{1}}之后问题就解决了。