1

私は.html.erbに持っています:

 <label for="form_marital_status">Marital Status:</label>
 <%= select("form", "marital_status", marital_status_options, {}, { }) %>

私のヘルパーmarital_status_optionsでは、次のように定義されています。

 def marital_status_options
      %w[Select Single Married Common-Law/Partner Divorced Widowed]
 end

marital_status_options選択で使用するキーと値のペアを定義できる方法はありますか?

4

1 に答える 1

4
%w[Select Single Married Common-Law/Partner Divorced Widowed]

これにより、各オプションのオプション値とテキストが同じになります。オプション値とテキストをオプションごとに異なるものにしたい場合は、配列の配列を返します。各配列の最初の値は、オプションのテキスト値です。2 番目はオプション値そのものです。

def marital_status_options
  [["Select", ""], ["Single", "single"], ["Married", "married"], ["Common-Law/Partner", "partners"], ["Divorced", "divorced"], ["Widowed", "widowed"]] 
end

これはドキュメントで明確に説明されています。

メソッド自体"Select"を介してこれを行う方法があるため、メソッドから空白のオプションを渡さないことも検討する必要があります。select

# Helper
def marital_status_options
  [["Single", "single"], ["Married", "married"], ["Common-Law/Partner", "partners"], ["Divorced", "divorced"], ["Widowed", "widowed"]] 
end

# form
<%= select("form", "marital_status", marital_status_options, {:include_blank => "Select"}) %>
于 2012-07-24T11:30:48.207 に答える