问题
I have a problem, I don't planned my database correctly, so always I need to use 'alternatives'.
I want pre-selected multiple values in select_tag. But I'm adding the vacancies 'manually' in table vacancy. My controller:
def create
@hr_curriculum_generic = HrCurriculumGeneric.new(params[:hr_curriculum_generic])
# Tabela CandidatosxVagas
unless params[:vacancy_ids].nil?
@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" %>
.....
Its works, but when I click in edit, the fields are not pre-selected.
Someone passed for a trouble like this and can help me?
回答1:
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.
来源:https://stackoverflow.com/questions/21667251/select-tag-with-multiple-values-pre-selected-values-inserted-manually-in-datab