使用Rails的Haml下拉菜单问题

时间:2014-10-21 17:16:29

标签: ruby-on-rails ruby haml

 form_for :radio, url(:radio, :add), :method => :put do |f|
    = f.error_messages
    %p
      = f.label :title, :caption => "Radio Name:"
      = f.text_field :title
    %p
      = f.select :city_name, XXXXXXXXXXXXXX
    %p
      = f.label :frequency
      = f.text_field :frequency
    %p
      = f.label :url
      = f.text_field :url
    %p
      = f.submit "Add", :class => 'button'

当我把XXXXXXXX作为

[['Hot','hot'],['Medium','medium'],['Cold','cold']]

甚至

City.all.map {|c| [c.city]}

我明白了:

can't convert Array into Hash

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

= f.select :city_name, City.all.map{ |c| [c.name, c.id] }试试这个

答案 1 :(得分:0)

您只需将选项包装在options_for_select中即可。例如:

= f.select :city_name, options_for_select([['Hot','hot'],['Medium','medium'],['Cold','cold']])

答案 2 :(得分:0)

data = %w/hot medium cold/.map { |s| [s.capitalize, s] }
= f.select :city_name, Hash[data]

<强>散列[数据]

答案 3 :(得分:0)

请在下面找到为我解决的问题

= f.select :city_name, :options => [["Delhi", "Delhi"], ["Mumbai", "Mumbai"], ["New York", "New York"], ["Punjab", "Punjab"]]

:options =&gt; 是关键......但是为什么文档没有提到这一点。可能是因为宝石版差异。

感谢大家的帮助。