:name方法表示在collection_select中不存在



我在Rails应用程序中安装了countries gem,并希望用它创建一个集合选择,如

  <%= f.collection_select :country, Country.all, :id, :name %>

但它给了我这个错误(对于第一个国家)。

undefined method `name' for ["Andorra", "AD"]:Array.

如果我在控制台上用这个宝石做Country.all,它会创建一系列像这样的国家

Country.all
=> [["Andorra", "AD"], ["United Arab Emirates", "AE"], ["Afghanistan", "AF"], ["Antigua and Barbuda", "AG"], ["Anguilla", "AI"], ["Albania", "AL"], ["Armenia", "AM"], ["Netherlands Antilles", "AN"], ["Angola", "AO"], ["Antarctica", "AQ"], ["Argentina", "AR"], ["American Samoa", "AS"], ["Austria", "AT"], ["Australia", "AU"], ["Aruba", "AW"], ["Åland Islands", "AX"], ["Azerbaijan", "AZ"], 

如果我创建一个像这样的国家传入的例子,它是两个字母的代码

>> c = Country.new('US')

然后我可以得到一个名称方法

>> c.name
=> "United States"

我正试图用countries宝石来做这件事,因为我不知道如何用countries_select宝石来创建集合选择。如果你不能告诉我如何解决countries gem的问题,那么请让我知道如何使用countries_select gem。

您可以安装gem'country_select'来提供呈现select标记的助手。传递模型和属性名称,它将渲染选择框。如果你有用户模型和国家属性在里面写

<%= country_select(:user, :country) %>

它将产生

<select id="user_country" name="user[country]"><option value="Afghanistan">Afghanistan</option>
<option value="Åland Islands">Åland Islands</option>
<option value="Albania">Albania</option>
....
</select>

https://github.com/hexorx/countries

https://github.com/stefanpenner/country_select