views:

21

answers:

3

I have a select tag nested in my form and I am needing to delete an item from my options_for_select array if it equals the word English

code:

<%= fields_for :users_languages do |u| %>
  <div class="field">
    <%= u.label :Assign_Languages %><br />
    <%= select_tag :language_id, 
      options_for_select(Language.all.collect {|lang|
        [lang.english, lang.id].delete_if {lang.english == "English"}
        }, @lang_list),
        :multiple => true,
        :prompt => 'Select Language' %>
  </div>
<% end %>

Problem: The above code works fine, but for some reason the first option is still shown in the mutli-select producing a blank select option. Is there anyway to get rid of the select option and its value? Am I even doing this correctly?

Thanks for all the help in advance!

A: 

You can do it using a collect and a reject.

Language.all.collect { |lang| [lang.english, lang.id] }.reject { |(lang, id)| lang == 'English' }

Not sure how to do it using just collect.

dj2
This works too :)
zetetic
A: 

It looks like your selection is actually returning an empty array as the first element. Try pulling out the unwanted option first:

Language.all.reject {|lang| lang.english == "English}.collect {|lang| [lang.english, lang.id]}
zetetic
A: 

Thanks for the reply guys! What I ended up doing was handle it in my query

code:

<%= fields_for :users_languages do |u| %>
        <div class="field">
            <%= u.label :Assign_Languages %><br />
            <%= select_tag :language_id, options_for_select(Language.find(:all, :conditions => "id != 1").collect {|lang| [lang.english, lang.id]}, @lang_list),:multiple => true, :prompt => 'Select Language' %>
         </div>
    <% end %>

the first row in the database will always be the same so this was easy to do in the query... although the suggestions above will come in handy in the future! Thanks again!

dennismonsewicz