我在Rails应用程序中安装了countries,并希望用它创建一个集合select,如下所示
<%= f.collection_select :country, Country.all, :id, :name %>但这给了我这个错误(对于第一个国家)。
undefined method `name' for ["Andorra", "AD"]:Array.如果我在控制台中使用这个gem执行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_select创业板创建一个集合选择。如果你不能告诉我如何解决国家创业板的问题,那么请让我知道如何使用countries_select创业板。
发布于 2013-08-17 18:16:50
您可以安装gem 'country_select‘来提供呈现选择标记的助手。您传递模型和属性名称,它呈现复选框。如果其中包含用户模型和country属性,则编写
<%= 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://stackoverflow.com/questions/18290857
复制相似问题