Program Tip

레일 선택 _ 태그 선택 값

programtip 2020. 12. 7. 20:32
반응형

레일 선택 _ 태그 선택 값


아래 주어진 코드의 경우 전달 된 값으로 선택 상자를 계속 선택하고 싶었습니다.

그러나 이것은 작동하지 않습니다.

@yrs =[2011,2010,2009,2008]
<%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,:selected=>2011) %>

어떻게해야하는지 알려주세요.

감사


:selected=>부품을 제거하십시오 .

통사론:

options_for_select(@options, @selected_options)

용법:

options_for_select(1..5, 3)  # creates a range 1..5 , with 3 as selected by default

<%= select_tag "page_type", options_for_select(@page_type.collect{ |u| [u.data_name, u.id]}, :selected=>@page.page_type), {:class =>"select_combobox",:onchange=>"reset_form(this.id,'page_type_msg');"} %>

이것은 나를 위해 작동합니다 :)


@M Tariq Aziz 대답을 명확히하기 위해 :

코드는 다음과 같아야합니다.

@yrs =[2011,2010,2009,2008]
<%= select_tag 'year', options_for_select([["Select" , "" ]] + @yrs.to_a,2011) %>

선택 태그의 일반적인 형식은 다음과 같습니다.

<%= select_tag 'year', options_for_select(:collection, :selected) %>

참고 URL : https://stackoverflow.com/questions/7347039/rails-select-tag-selected-value

반응형