期望Rails,得到数组

时间:2016-11-15 01:00:03

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4

我希望从数组中获取一个id,当它获取时会在动作上显示这个添加Order.new

  

颜色(#70131258622840)预期,得到阵列(#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

2 个答案:

答案 0 :(得分:2)

table

这一行给你一系列颜色,而不是颜色。这会更自然

tr

但是var lastDate = doc.DocumentNode.Descendants().Where(a => a.Name == "td" && a.InnerText.Contains("Last"));也为你提供了一个数组,在这种情况下甚至是一个元素。 color = product.colors.select { |i| [i.id] } 为您提供所需的元素或color = product.colors.select { |i| i.id } 代替

select

答案 1 :(得分:0)

正如你所说,你需要一系列的ID。你也可以product.colors.ids

这将返回颜色ID数组。