我的应用程序中有很多产品。用户可以选择其中一些。然后我用选择的产品生成部分。我为每个选定的产品指定了一个名称,因为我希望通过params []在我的控制器中访问它们。
我的问题是:如何创建所有选定产品的哈希值?然后我可以得到像params [:selected-products]这样的产品而不是params [:quantity-id]。可能吗? 我不确定我是否可以使用任何form_for,因为我的视图很复杂(有一个form_for等)。
<% @chosen_products.each do |product| %>
<input name="quantity-<%=product.id %>" id="quantity-<%=product.id %>" class="product-quantity" type="number" step="0.01" >
<% end %>
@ chosen_prodcuts.inspect for 2 products:
[#<Product id: 6, name: "Jogurt", kalorycznosc: 100.0, woda: 2.0, bialko: 2.0, bialkoZwierzece: 2.0, bialkoRoslinne: 2.0, tluszcz: 2.0, weglowodany: 2.0, blonnik: 2.0, laktoza: 2.0, sacharoza: 2.0, cholesterol: 2.0, sod: 2.0, potas: 2.0, wapn: 2.0, fosfor: 2.0, magnez: 2.0, zelazo: 2.0, cynk: 2.0, miedz: 2.0, mangan: 2.0, retinol: 2.0, betaKaroten: 2.0, witD: 2.0, witE: 2.0, witB1: 2.0, witB2: 2.0, witB3: 2.0, witB6: 2.0, foliany: 2.0, witB12: 2.0, witC: 2.0, fenyloalanina: 2.0, ogolemNasyconeKwTl: 2.0, ogolemJednoKwTl: 2.0, ogoleWieloKwTl: 2.0, stosunekWieloKwTl: 2.0, measure: nil, conversion: nil, alias: nil, user_id: 1, group_id: 2, created_at: "2015-11-26 23:01:57", updated_at: "2015-11-27 22:28:02">, #<Product id: 7, name: "Ser biały2", kalorycznosc: 200.0, woda: nil, bialko: nil, bialkoZwierzece: nil, bialkoRoslinne: nil, tluszcz: nil, weglowodany: nil, blonnik: nil, laktoza: nil, sacharoza: nil, cholesterol: nil, sod: nil, potas: nil, wapn: nil, fosfor: nil, magnez: nil, zelazo: nil, cynk: nil, miedz: nil, mangan: nil, retinol: nil, betaKaroten: nil, witD: nil, witE: nil, witB1: nil, witB2: nil, witB3: nil, witB6: nil, foliany: nil, witB12: nil, witC: nil, fenyloalanina: nil, ogolemNasyconeKwTl: nil, ogolemJednoKwTl: nil, ogoleWieloKwTl: nil, stosunekWieloKwTl: nil, measure: "porcja", conversion: 70.0, alias: "twarożek", user_id: 1, group_id: 2, created_at: "2015-11-26 23:01:58", updated_at: "2015-11-27 21:54:06">]
Product.rb(安静长):
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :group
has_many :ingredients
has_many :meals, :through => :ingredients
validates :user_id, presence: true
validates :name, presence: true, length: {maximum: 50}
validates :alias, length: {maximum: 50}
validates :measure, length: {maximum: 50}
validates :conversion, :numericality => true, :allow_nil => true
validates :kalorycznosc, :numericality => true, :allow_nil => true
validates :woda, :numericality => true, :allow_nil => true
validates :bialko, :numericality => true, :allow_nil => true
validates :bialkoRoslinne, :numericality => true, :allow_nil => true
validates :bialkoZwierzece, :numericality => true, :allow_nil => true
validates :tluszcz, :numericality => true, :allow_nil => true
validates :weglowodany, :numericality => true, :allow_nil => true
validates :blonnik, :numericality => true, :allow_nil => true
validates :laktoza, :numericality => true, :allow_nil => true
validates :sacharoza, :numericality => true, :allow_nil => true
validates :cholesterol, :numericality => true, :allow_nil => true
validates :sod, :numericality => true, :allow_nil => true
validates :potas, :numericality => true, :allow_nil => true
validates :wapn, :numericality => true, :allow_nil => true
validates :fosfor, :numericality => true, :allow_nil => true
validates :magnez, :numericality => true, :allow_nil => true
validates :zelazo, :numericality => true, :allow_nil => true
validates :cynk, :numericality => true, :allow_nil => true
validates :miedz, :numericality => true, :allow_nil => true
validates :mangan, :numericality => true, :allow_nil => true
validates :retinol, :numericality => true, :allow_nil => true
validates :betaKaroten, :numericality => true, :allow_nil => true
validates :witB1, :numericality => true, :allow_nil => true
validates :witB2, :numericality => true, :allow_nil => true
validates :witB3, :numericality => true, :allow_nil => true
validates :witB6, :numericality => true, :allow_nil => true
validates :witB12, :numericality => true, :allow_nil => true
validates :witC, :numericality => true, :allow_nil => true
validates :witD, :numericality => true, :allow_nil => true
validates :witE, :numericality => true, :allow_nil => true
validates :foliany, :numericality => true, :allow_nil => true
validates :fenyloalanina, :numericality => true, :allow_nil => true
validates :ogolemJednoKwTl, :numericality => true, :allow_nil => true
validates :ogolemNasyconeKwTl, :numericality => true, :allow_nil => true
validates :ogoleWieloKwTl, :numericality => true, :allow_nil => true
validates :stosunekWieloKwTl, :numericality => true, :allow_nil => true
end
答案 0 :(得分:0)
你可以试试这个:
<% chosen_products_csv = @chosen_products.map{|p| p.id }.join(',') %>
<input name="chosen_products" id="chosen_products class="product-quantity" type="text" step="0.01" value="<%= chosen_products_csv %>" >
它将为您提供一个输入,其值为逗号分隔的产品ID列表。
然后在您的控制器中,您可以检索产品:
unless params['chosen_products'].blank?
product_ids = params['chosen_products'].split(',').map{|p| p.to_i }
@products = Product.where(id: product_ids) if product_ids.length > 1 || product_ids.first != 0
end
您没有指定如何将这些所选产品分配给表单,但对于上面的示例,您可以将其作为隐藏字段,并在表单子目录之前通过javascript添加值。