2016-07-31 1 views
0

(레일 양식),이 있습니다. 예 : 재료가 같은 이름을 가지고 있기 때문에인쇄 두 값 내 양식에 대한

Cat 
Cat 

그러나, 이것은 도움이되지 않습니다. Material 레코드에 color라는 또 다른 속성이 있습니다 : color.

가 나는

Cat - Brown 
Cat - Orange 

가 어떻게이 일을 가야합니까 드롭 다운이를 인쇄 할? 대신 메서드 호출을 시도했지만 원하는 방식으로 출력하지 않습니다. 여기 내가 한 일이있다.

View:  
<%= tag_field.collection_select(:id, Material.order(:name), :id, :something, 
     :prompt => "-select-")%> 

Model: 
def something 
    materials_array = [] 
    Material.all.each do |material| 
     if material.color == nil 
     material.name + '-' + material.size 
     else 
     materials_array.push(material.name + '-' + material.color) 
     end 
    end 
    materials_array 
    end 

그러나, 이와 같은 인쇄 드롭 아웃 :

["Cat - Brown", "Cat - Orange"] 
["Cat - Brown", "Cat - Orange"] 

동일한 값, 두 출력한다. 내가 가까이있는 것 같니? 도와주세요.

답변

0

collection_select 대신 select을 사용하면 더 쉽다고 생각합니다. 그것을 시도 :

<%= tag_field.select :id, Material.order(:name).map{ |m| [ "#{m.name} - #{m. color}", m.id ] }, {prompt: "-select-"} %> 
0

This answer 명확하게 collection_select 도우미의 사용을 설명합니다. 당신이 결과를 얻는 경우

:name_with_initial, # this is name of method that will be called for 
# every row, result will be set as value 

# as a result, every option will be generated by the following rule: 
# <option value=#{author.id}>#{author.name_with_initial}</option> 
# 'author' is an element in the collection or array 

그래서 두 번 컬렉션/배열 중복 값이 ​​의미에있어서 :name_with_initial은 (코드에서 방법 something에 해당)을 설명한다.