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.
options_from_collection_for_select has 4 parameters:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With