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

∥☆過路亽.° 提交于 2019-12-10 04:24:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!