我希望从数组中获得一个id,当它显示在动作中时,添加Order.new
颜色(#70131258622840),got数组(#70131401174240)
有人知道为什么吗?
产品模型
has_many :colorships
has_many :colors, through: :colorships颜色模型
has_many :colorships
has_many :products, :through => :colorships乘积控制器
def new
Product.New
@dropdown = @product.colors.collect { |co| [co.name, co.id] }
end
def show
Product.find(params[:id])
color = product.colors.select { |i| [i.id] }
end
def add
product = Product.find(params[:id])
if product
color = product.colors.select { |i| [i.id] }
if order.nil? # create new order
order = Order.new
order.product = product
order.color = color
end
end
end发布于 2016-11-15 01:17:03
color = product.colors.select { |i| [i.id] } 这一行是给你一系列的颜色,而不是颜色。这样就更自然了
color = product.colors.select { |i| i.id } 但是select也为您提供了一个数组,即使在本例中只有一个元素。find只为您提供您想要的元素或nil
color = product.colors.find { |i| i.id } 发布于 2016-11-15 06:14:30
就像你说的,你需要一系列的ids。您也可以通过product.colors.ids。
这将返回颜色ids数组。
https://stackoverflow.com/questions/40600387
复制相似问题