【问题标题】:Rails - own hash inside params[]Rails - 参数内的自己的哈希 []
【发布时间】:2015-11-28 15:36:56
【问题描述】:

我的应用程序中有很多产品。用户可以选择其中的一些。然后我用选择的产品生成部分。我为每个选择的产品分配了一个名称,因为我想通过 params[] 在我的控制器中访问它们。

我的问题是:如何使用所有选定的产品创建类似于哈希的东西?然后我可以得到像 params[:chosen-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 2 个产品:

[#<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

【问题讨论】:

  • 你能粘贴@chosen_products 使用inspect 的样子吗?同时粘贴产品型号。
  • 我建议您使用Rails' form helpers,因为您的第一个代码示例中的input 看起来有点乱。例如:&lt;%= number_field_tag :"quantity-#{product.id}", class: 'product-quantity', step: '0.01' %&gt;。这会生成相同的 HTML 标记,但更简洁。

标签: ruby-on-rails params


【解决方案1】:

你可以试试这个:

<% 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 添加值。

【讨论】:

  • 事实上,我的控制器中有这个值(chosen_products),但在不同的操作中(由 ajax 触发)。是否也可以在创建操作中获取此值?
  • 如果params['chosen_products'] 为零?我肯定会说这是一个非常危险的答案!你可以写得更好。为什么不打一个不同的电话并在里面安全检查?
  • 当然,你是对的,这意味着它是如何实现的,因为 OP 没有提供任何适合该解决方案的控制器代码。反正我已经按照你的建议改了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 2013-03-05
  • 2014-05-24
  • 2017-08-08
  • 2019-04-15
  • 2018-04-24
相关资源
最近更新 更多