Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select tag with multiple values pre-selected - Values inserted manually in database

I need to pre-select multiple values in a select_tag. But I'm adding the vacancies 'manually' in table vacancies as follows:

My controller:

def create
  @hr_curriculum_generic = HrCurriculumGeneric.new(params[:hr_curriculum_generic])

  if params[:vacancy_ids].present?
    @vacancies_ids = params[:vacancy_ids]

-- my form:

  @vacancies_ids.each do |vacancy_id|
    # Armazena os id do curriculum, vaga e do cargo na tabela CandidatosxVagas
    @candidates_vacancies = CandidatesVacancy.new 
    <% @vacancies = Vacancy.all %>
    <%= select_tag "vacancy_ids[]", options_from_collection_for_select(Vacancy.all, "id", "title"), :multiple => true, :id => "vacancy_ids", :class => "form-control" %>

.....

It works, but when I click in the edit btn, the fields are not being pre-selected.

like image 552
Lorena Bento Avatar asked Feb 10 '14 00:02

Lorena Bento


1 Answers

options_from_collection_for_select has 4 parameters:

  • collection
  • id
  • column
  • selected

You can provide a single value, or a hash to denote selected values. Try this:

<%= select_tag "vacancy_ids[]", options_from_collection_for_select(Vacancy.all,"id","title",{:selected=>[1,2,3,4]})), :multiple => true, :id => "vacancy_ids", :class => "form-control" %>

I'm not sure where the values you are trying to select come from but pipe them into the selected hash.

like image 62
eabraham Avatar answered Sep 22 '22 07:09

eabraham