Rails 4-国家/地区选择简单表单(&A) [英] Rails 4 - Country Select & Simple Form
问题描述
我正在尝试使用rails 4开发一个应用程序。我对表单使用简单表单,对国家/地区列表使用COUNTRY_SELECT GEM。
我有一个地址模型,其中包含这个方法:
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
当我尝试保存新地址时,收到以下错误:
undefined method `translations' for "Australia":String
有人能看到此方法定义有什么问题吗?
如果我将我的视图更改为:
<% if @profile.addresses.any? %>
<%= @profile.addresses.first.country.titlecase %>
<% else %>
<span class="profileeditlink">
<%= link_to "Add your location", new_address_path %>
</span>
<% end %>
然后记录显示-但显示为AU而不是Australia(这是地址模型中的方法提供的内容)。
地址表:
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 "time_zone"
t.float "latitude"
t.float "longitude"
t.integer "addressable_id"
t.integer "addressable_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "addresses", ["addressable_type", "addressable_id"], name: "index_addresses_on_addressable_type_and_addressable_id", unique: true, using: :btree
采纳JAEHYEN的建议
我将地址模型中的国家/地区名称方法更改为:
def country_name
# self.country = ISO3166::Country(country)
# country.translations[I18n.locale.to_s] || country.name
iso_country = ISO3166::Country.find_by_name[country] # `country` should be name like 'Australia'
iso_country.translations[I18n.locale.to_s] || iso_country.name
end
我收到此错误:
undefined method `translations' for nil:NilClass
另一次尝试:
我尝试将表单输入更改为:
<%= f.country_select :country, priority: [ "Australia", "New Zealand", "United Kingdom" ] %>
它仍然只显示国家代码而不是国家名称。我卡住了。
另一次尝试
我发现了这个帖子: Rails Simple_Form: How to show long name of the country
此帖子中的答案建议将Country_NAME定义为:
def country_name
country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name
end
这与我以前的尝试略有不同,但是,当我尝试此操作时,收到以下错误:
undefined local variable or method `country_code' for #<Address:0x007fbae5bfb290>
我尝试将方法更改为:
def country_name
self.country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name
end
这与不使用‘self’的公式产生相同的错误。我认为这些尝试不起作用,因为我的地址表中的属性称为‘Country’。
当我将方法更改为:
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
我发现单词‘Translations’有错误。当我从该方法中删除‘.Translations’时,我得到一个与单词‘name’相关的错误。
试图弄清楚这件事,我简直要发疯了。
另一次尝试
我尝试将国家创业板添加到我的创业板文件中(在COUNTRY_SELECT上方)。
当我捆绑这个宝石时,没有任何变化。同样的问题。
另一次尝试
重试(与我最初定义的方法相同),但安装了国家/地区GEM(在COUNTRY_SELECT GEM上方):
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
我收到此错误:未定义"开曼群岛"的方法`Translations‘:字符串
这与我最初开始时遇到的问题相同,因此我不认为添加国家/地区创业板有助于解决问题。
推荐答案
此代码适合我。我的用户表中有Country列。 在我的型号上:-
def country_name
country = self.country
ISO3166::Country[country]
end
格式:-
<%= form_for @user do |f| %>
<%= country_select("user", "country") %>
<% end %>
这篇关于Rails 4-国家/地区选择简单表单(&A)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!