1

次のコードを含む collection_select フォームがあります。

<%= f.collection_select(:city_id,  City.order('name ASC'),  :id, :name, {:prompt   => "Select a City"}, {:id => 'cities_select'}) %>

現在、データベース内のすべての都市を名前でリストしているだけですが、都市名が重複しています。たとえば、2 つの「ピオリア」都市があります。

そのため、都市を区別しやすくするために、州属性を「イリノイ州ピオリア」のようにカンマで区切って表示したいと思います。このコードをフォーム コード内に記述するにはどうすればよいですか?

4

3 に答える 3

6

collection_selectそのためにも使えます

f.collection_select :city_id, City.order('name ASC'), :id, :name_with_state, { prompt: 'Select a City' }, { id: 'cities_select' }

name_with_stateあなたの都市モデルにあるはずです

def name_with_state
  "#{name}, #{state}"
end
于 2013-05-24T04:58:42.977 に答える
0

選択を使用できます:

<%= f.select(:city_id,  City.order('name ASC').map{ |city| [city.your_method, city.id]},
      {:prompt   => "Select a City"}, {:id => 'cities_select'}) %>
于 2013-05-24T04:06:24.537 に答える