# Select\_tag helper. How do I edit collection values?

**URL:** <https://www.ruby-forum.com/t/select-tag-helper-how-do-i-edit-collection-values/145087>\
**Category:** Rails\
**Created:** [August 5, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/select-tag-helper-how-do-i-edit-collection-values/145087 "2008-08-05T21:16:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [August 5, 2008, 9:16pm UTC](https://www.ruby-forum.com/t/select-tag-helper-how-do-i-edit-collection-values/145087/1 "2008-08-05T21:16:04Z")

</div>

Hi everyone,

I am using a collection in a select\_tag to select categories (uses) for  
my object (item). I have sub categories so I want some way to  
distinguish between the parent and child categories in the select list.  
What I have works fine but is confusing to look at because it is just a  
long list in the select box.

Here is what I have:

\<%= select\_tag ‘item[use\_ids][]’,  
options\_from\_collection\_for\_select(@uses, :id, :name,  
@item.uses.collect{|use| use.id}), { :multiple =\> true, :size =\> 20 } %\>

Here is what it looks like:

[http://robert.naranjastudio.com/select\_tag.png](http://robert.naranjastudio.com/select_tag.png)

I want to format the values so the items in the select box look  
something like:

Backpacks  
-Computer Backpacks  
-Outdoor & Sports  
-Wheeled  
Brand Name  
-Cutter & Buck  
-High Sierra

Kind of like that. Does anyone know how to work that magic in the  
select\_tag helper using a collection like I am? Is it possible to maybe  
add a “-” in the value displayed for the sub uses?

Thanks in advance for your help!!

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [August 5, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/select-tag-helper-how-do-i-edit-collection-values/145087/2 "2008-08-05T21:29:03Z")

</div>

On 5 Aug 2008, at 20:16, Robert Merrill wrote:

> I want to format the values so the items in the select box look  
> Kind of like that. Does anyone know how to work that magic in the  
> select\_tag helper using a collection like I am? Is it possible to  
> maybe  
> add a “-” in the value displayed for the sub uses?

Have you tried using option\_groups\_from\_collection\_for\_select instead  
of options\_from\_collection\_for\_select?  
One way of having a - added in front would be to have a method on that  
model:

Fred

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [August 5, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/select-tag-helper-how-do-i-edit-collection-values/145087/3 "2008-08-05T22:16:51Z")

</div>

Thanks Fred,

I will look into it more. I think I might have a problem using  
option\_groups\_from\_collection\_for\_select though, it looks like if I use  
it I will not be able to put items into the main categories, only child  
categories. Maybe not though. So you think adding a method to the item  
model that would return the name + “-” would work? How would call it?  
Like this:

\<%= select\_tag ‘item[use\_ids][]’,  
options\_from\_collection\_for\_select(@uses, :id, :name\_with\_dash,  
@item.uses.collect{|use| use.id}), { :multiple =\> true, :size =\> 20 } %\>

~Robert

---

<div class="post-metadata">

**Author:** ![newbee-robert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dbc845/32.png) [@newbee-robert](https://www.ruby-forum.com/u/newbee-robert)\
**Post date:** [August 5, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/select-tag-helper-how-do-i-edit-collection-values/145087/4 "2008-08-05T22:23:56Z")

</div>

> \<%= select\_tag ‘item[use\_ids][]’,  
> options\_from\_collection\_for\_select(@uses, :id, :name\_with\_dash,  
> @item.uses.collect{|use| use.id}), { :multiple =\> true, :size =\> 20 } %\>

That worked like a charm, exactly what I wanted.

Here is what my model method looks like

def name  
“#{use\_name}”  
end

def name\_with\_dash

```
if self.parent_id != nil
  "--#{use_name}"
else
  name
end

```

end

Thanks again!!
