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