当我尝试访问下面的@ line_item.id实例变量id时,我收到错误 - (当我记录调试值时为nil)。但[:id]工作正常 那是为什么?
@cart = current_cart
@line_item = @cart.line_items.find_by_id(params[:id])
@line_item = @line_item.decrement_quantity(@line_item[:id])
答案 0 :(得分:0)
正如@Kyle所说,@ line_item.id和@line_item [:id]应该返回相同的值。
另外一个,因为你在@line_item
对象上调用实例方法,你不需要
@line_item.id
作为方法的参数。您可以直接访问方法内的self.id
我建议写一些像 -
@cart = current_cart
@line_item = @cart.line_items.find_by_id(params[:id])
@line_item.decrement_quantity
## inside model
class LineItem <
def decrement_quantity
puts self.id # will print the id of object calling method
end
end