我正在尝试在 Rails 4 中制作一个应用程序。我对表单使用简单的表单,对国家/地区列表使用 country_select gem。
我有一个地址模型,其中包括这个方法:
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
Run Code Online (Sandbox Code Playgroud)
当我尝试保存新地址时,出现此错误:
undefined method `translations' for "Australia":String
Run Code Online (Sandbox Code Playgroud)
谁能看出这个方法定义有什么问题?
如果我将视图更改为:
<% if @profile.addresses.any? %>
<%= @profile.addresses.first.country.titlecase %>
<% else %>
<span class="profileeditlink">
<%= link_to "Add your location", new_address_path %>
</span>
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后记录显示 - 但显示为 AU 而不是澳大利亚(这是地址模型中提供的方法)。
地址表:
create_table "addresses", force: :cascade do |t|
t.string "unit"
t.string "building"
t.string "street_number"
t.string "street"
t.string "city"
t.string "region"
t.string "zip"
t.string "country"
t.boolean "main_address"
t.boolean "project_offsite"
t.string …Run Code Online (Sandbox Code Playgroud)