# Help with Create and Update with options\_for\_select in a sel

**URL:** <https://www.ruby-forum.com/t/help-with-create-and-update-with-options-for-select-in-a-sel/105023>\
**Category:** Rails\
**Created:** [May 25, 2007, 1:01am UTC](https://www.ruby-forum.com/t/help-with-create-and-update-with-options-for-select-in-a-sel/105023 "2007-05-25T01:01:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [May 25, 2007, 1:01am UTC](https://www.ruby-forum.com/t/help-with-create-and-update-with-options-for-select-in-a-sel/105023/1 "2007-05-25T01:01:35Z")

</div>

I have a select\_tag in my view that uses options\_for\_select with  
multiple = true. I am having trouble figuring out the Rails way to  
create and update that field. Please help.

## Models:

Service has\_many AccessControl  
AccessControl belongs\_to Service

## application.rb

$types = Array[“Athens”, “htpasswd”, “IP”, “None”, “Other”,  
“Password”, “Shibboleth”]

## View:

\<% if @service.access\_controls  
@types = @service.access\_controls.collect{|a| a.value}.join(’ ')  
end %\>  
\*\</  
span\>Access Control:  
\<%=select\_tag ‘access\_control[]’,  
options\_for\_select($types, ‘@types’), {:multiple =\> true, :size  
=\> 5, :id =\> “access\_control\_value”,  
:onfocus=\>“fixedtooltip(CONTROL\_MSSG, this, event,  
‘’)”, :onblur=\>“delayhidetip()”}%\>  
\<%= error\_message\_on :service, :access\_controls %\>

## Controller

#create works but is this the best way?  
def create  
…  
if params[:access\_control]  
params[:access\_control].each do |value|  
control = AccessControl.new  
control.value = value  
control.service\_id = @service.id  
control.save  
@service.access\_controls \<\< control  
end  
end  
…  
end

def update  
??? I have no idea how to update the access\_controls fields  
end

Thanks in advance, K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [May 25, 2007, 1:48am UTC](https://www.ruby-forum.com/t/help-with-create-and-update-with-options-for-select-in-a-sel/105023/2 "2007-05-25T01:48:39Z")

</div>

Hey Kim, what you can do:

HTML:

 \<% for party in @parties %\> \<%= party.name %\> \<% end %\>

The Rails Way:

select( “party”, “id”, Party.find(:all).collect {|p| [p.name, p.id]  
}, { :multiple =\> true, :size =\> 3 } )

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [May 25, 2007, 2:03am UTC](https://www.ruby-forum.com/t/help-with-create-and-update-with-options-for-select-in-a-sel/105023/3 "2007-05-25T02:03:41Z")

</div>

Hi Conrad,  
Thanks, but no, that will not work.

First:  
select( “party”, “id”, Party.find(:all).collect {|p| [ p.name,  
p.id ]}, { :multiple =\> true, :size =\> 3 } )

select can not do a multiple select. Yes you can put :multiple =\> true  
in it, and in the UI it will look like it is multi-selecting things,  
but it actual does not send more then one thing in the params hash.  
Disagree? Prove me wrong.

Second:  
I have seen plenty of code examples for doing this when the options  
are coming from a database (just like your example), but if you look  
at my post, you will see that I do not have that case. I have a hash  
of static options, not options from a database.

Thanks anyways.

Any other ideas? K

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [May 25, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/help-with-create-and-update-with-options-for-select-in-a-sel/105023/4 "2007-05-25T21:15:36Z")

</div>

I have not been able to find any examples. Anyone know how to do this?
