는이 같은 람다와 함께 value_method 속성을 사용할 수 있습니다 : 이것처럼
<%= form.input :department_short, label: I18n.t('department'), :value_method => lambda {|t| "#{t.name} - #{t.code}"}, wrapper_html: {class: 'header'},
placeholder: 'Select department', as: :collection_select, collection: @departments, required: false %>
이 보낸해야한다 "이름 - 코드"값을 사용하면 객체에서, 예제. 람다 작업에서는 원하는대로 객체에서 얻은 값을 조작 할 수 있습니다. 당신이 아이디어를 얻고 이것이 도움이되기를 바랍니다. 행운을 빕니다! 공식 문서 (https://github.com/plataformatec/simple_form)에서
:
편집
Collection inputs accept two other options beside collections:
label_method => the label method to be applied to the collection to retrieve the label (use this instead of the text_method option in collection_select)
value_method => the value method to be applied to the collection to retrieve the value
또한 당신이 당신의 모델에 방법 to_label
이있는 경우,이 라벨을 위해 그 방법을 사용하는 것으로 보인다, 당신이 그렇게 돈 label_method를 사용할 때마다 label_method를 넣어야합니다. 의사는 비슷한 to_value
방법이 있는지는 언급하지 않지만 확실히 좋을 것입니다.
정말 감사합니다. 여기에있는 동안 드롭 다운의 표시 이름을 수정하는 비슷한 방법이 있습니까? –
@ap 그래, label_method는 같은 방법으로 작동하므로 라벨을 바꾸기 위해 사용할 수 있습니다. –
당신은 그런 왕자입니다. (이것들이 공식적인 문서에 있다면 좋을 것입니까, 아니면 어딘가에 놓치고 있습니까?) –