From Chapter 12 Task 4: Capturing an order.
第12章任务4:捕获订单。
Where did the @cart parameter from @order.add_line_items_from_cart(@cart) that is in the Orders Controller come from and what values are being passed through that method?
来自@order.add_line_items_from_cart(@cart)的@cart参数来自于订单控制器中,哪些值是通过该方法传递的?
class OrdersController < ApplicationController
def create
@order = Order.new(order_params)
@order.add_line_items_from_cart(@cart)
end
class Order < ActiveRecord::Base
has_many :line_items, dependent: :destroy
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end
LineItem has one-to-many association to both Cart and Order. Since LineItem has an association to both Cart and Order, does that mean that it has access to the instance variable in the Cart and Order controller?
LineItem对购物车和订单都有一对多的关联。由于LineItem与Cart和Order都有关联,这是否意味着它可以访问Cart和Order控制器中的实例变量?
How are they able to access or use the @cart instance variable from other controller that isn't Cart
他们如何能够访问或使用不是Cart的其他控制器的@cart实例变量?
Additional question
额外的问题
What values are being appended to line_items from 'item'
从“项”中附加到line_items的值是什么
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
I'm really new to Rails, so I'm not really sure if I'm even asking the right question.
我对Rails很陌生,所以我不确定我是否问对了问题。
1 个解决方案
#1
0
If you do an application-wide search for '@cart' you will most surely find where @cart is being defined. Unless you're using a gem, my bet is that it is being defined in a helper method in one of your controllers. If @cart is used by multiple controllers (makes sense that it would be used by orders_controller, maybe users_controller, maybe products_controller), it might be worth checking in application_controller.
如果你在应用程序范围内搜索“@cart”,你肯定会发现@cart的定义。除非你使用的是gem,我打赌它是在你的控制器中的一个辅助方法中定义的。如果@cart被多个控制器使用(它会被orders_controller,可能是users_controller,或者products_controller)使用,那么它可能值得在application_controller中进行检查。
#1
0
If you do an application-wide search for '@cart' you will most surely find where @cart is being defined. Unless you're using a gem, my bet is that it is being defined in a helper method in one of your controllers. If @cart is used by multiple controllers (makes sense that it would be used by orders_controller, maybe users_controller, maybe products_controller), it might be worth checking in application_controller.
如果你在应用程序范围内搜索“@cart”,你肯定会发现@cart的定义。除非你使用的是gem,我打赌它是在你的控制器中的一个辅助方法中定义的。如果@cart被多个控制器使用(它会被orders_controller,可能是users_controller,或者products_controller)使用,那么它可能值得在application_controller中进行检查。