Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top