Kim
2007-May-24 23:00 UTC
Help with Create and Update with options_for_select in a select_tag
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 %>
<label for="access_control_value"><span class =
"required">*</
span>Access Control:</label>
<%=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 %><br />
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Conrad Taylor
2007-May-24 23:45 UTC
Re: Help with Create and Update with options_for_select in a select_tag
Hey Kim, what you can do:
HTML:
<select name="parties[]" multiple size="3" >
<% for party in @parties %>
<option value="<%= party.id %>"><%=
party.name %></option>
<% end %>
</select>
The Rails Way:
select( "party", "id", Party.find(:all).collect {|p| [
p.name, p.id ]
}, { :multiple => true, :size => 3 } )
Good luck,
-Conrad
On 5/24/07, Kim <Kim.Griggs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
> 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 %>
> <label for="access_control_value"><span class =
"required">*</
> span>Access Control:</label>
> <%=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 %><br />
>
> 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
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Kim
2007-May-25 00:02 UTC
Re: Help with Create and Update with options_for_select in a select_tag
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
On May 24, 4:45 pm, "Conrad Taylor"
<conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hey Kim, what you can do:
>
> HTML:
>
> <select name="parties[]" multiple size="3" >
> <% for party in @parties %>
> <option value="<%= party.id %>"><%=
party.name %></option>
> <% end %>
> </select>
>
> The Rails Way:
>
> select( "party", "id", Party.find(:all).collect {|p| [
p.name, p.id ]
>
> }, { :multiple => true, :size => 3 } )
>
> Good luck,
>
> -Conrad
>
> On 5/24/07, Kim <Kim.Gri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
>
>
>
> > 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 %>
> > <label for="access_control_value"><span class =
"required">*</
> > span>Access Control:</label>
> > <%=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 %><br />
>
> > 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Kim
2007-May-25 19:14 UTC
Re: Help with Create and Update with options_for_select in a select_tag
I have not been able to find any examples. Anyone know how to do this? On May 24, 5:02 pm, Kim <Kim.Gri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On May 24, 4:45 pm, "Conrad Taylor" <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hey Kim, what you can do: > > > HTML: > > > <select name="parties[]" multiple size="3" > > > <% for party in @parties %> > > <option value="<%= party.id %>"><%= party.name %></option> > > <% end %> > > </select> > > > The Rails Way: > > > select( "party", "id", Party.find(:all).collect {|p| [ p.name, p.id ] > > > }, { :multiple => true, :size => 3 } ) > > > Good luck, > > > -Conrad > > > On 5/24/07, Kim <Kim.Gri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 %> > > > <label for="access_control_value"><span class = "required">*</ > > > span>Access Control:</label> > > > <%=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 %><br /> > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Seemingly Similar Threads
- How do I show the selected values in options_for_select? Not as simple as it sounds.
- Help using options_for_select in select_tag
- Select_tag with options_for_select not populating DB.
- A copy of XX has been removed from the module tree but is still active!
- options_for_select - how to set the selected value?