开发者

pre selecting items in select box rails

开发者 https://www.devze.com 2023-01-04 04:18 出处:网络
UPDATE @selected attributes: group_id: \"29\" attributes_cache: {} @data attributes: created_at: 2010-06-19 10:16:13

UPDATE

@selected
attributes: 
group_id: "29"
attributes_cache: {}
@data
attributes: 
created_at: 2010-06-19 10:16:13
term_id: "1"
updated_at: 2010-06-19 10:16:13
id: "29"
course_id: "1"

Hi,

I am trying to pre-select items within a select_tag

 <%= select_tag "contact[group_ids][]",
       options_for_select(
         @data.map{ |d| [" Term #{d.term.number} #{d.term.start_date} #{d.course.course_type} #{d.cour开发者_如何学JAVAse.course_name}"] },
         @selected.map{ |j| j.id }
       ),
       :multiple => true
%>

The @data object is all the items in the list and @selected contains the id's of the ones that should be selected.

Any ideas why they are not being selected ?

Thanks, Alex


I believe it's just

<%= select_tag "contact[group_ids][]",
       options_for_select(
         @data.map{ |d| [" Term #{d.term.number} #{d.term.start_date} #{d.course.course_type} #{d.course.course_name}"] },
         @selected
       ),
       :multiple => true
%>

because railsapi.com says:

"selected may also be an array of values to be selected when using a multiple select"

Edit

I thought @selected was an array of IDs, but it isn't. So the way you first write your code (with @selected.map{ |j| j.id }) should work.


You can do this by using - options_from_collection_for_select()

http://shiningthrough.co.uk/Select-helper-methods-in-Ruby-on-Rails

0

精彩评论

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