开发者

how to show concatenated information (from relationship) in collection_select

开发者 https://www.devze.com 2022-12-20 07:51 出处:网络
I am trying to show concatenated information in the drop downs text. This is what I have: <%=collection_select(:product, \'prod_name\', @prods, :id,

I am trying to show concatenated information in the drop downs text.

This is what I have:

<%=collection_select(:product, 'prod_name', @prods, :id, 
"#{:category.name - :prod_name}", {:prompt => 'Select Product'})%>

Product belongs_to category.

So i can do the following which works fine:

@p = Product.all
@p.first.prod_name
@p.first.category.name

So eventually i want a drop down that has categoryname - product name

For example: if category name is Edible and product name is Brownie th开发者_开发问答en in drop down i want Edible - Brownie


There's a couple of different ways of doing it, but I would define a new instance method on the product model like:

def category_product_name
  "#{self.category.name} - #{self.prod_name}"
end

Then you can use this in your view:

<%=collection_select(:product, 'prod_name', @prods, :id, :category_product_name, {:prompt => 'Select Product'})%>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号