Mel*_*Mel 0 ruby-on-rails country-codes simple-form
我正在尝试在 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 "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
Run Code Online (Sandbox Code Playgroud)
接受 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
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
undefined method `translations' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
另一种尝试:
我找到了这个资源:http : //www.scriptscoop.net/t/4ee6d5ef4577/displaying-countries-using-country-select-gem-in-rails-4.html
我尝试将表单输入更改为:
<%= f.country_select :country, priority: [ "Australia", "New Zealand", "United Kingdom" ] %>
Run Code Online (Sandbox Code Playgroud)
它仍然只显示国家代码而不是国家名称。我被困住了。
另一种尝试
我找到了这篇文章: 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
Run Code Online (Sandbox Code Playgroud)
这与我之前的尝试略有不同,但是,当我尝试此操作时,出现此错误:
undefined local variable or method `country_code' for #<Address:0x007fbae5bfb290>
Run Code Online (Sandbox Code Playgroud)
我尝试将方法更改为:
def country_name
self.country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name
end
Run Code Online (Sandbox Code Playgroud)
这与不使用“self”的公式产生相同的错误。我认为这些尝试不起作用,因为我的地址表中的属性称为“国家/地区”。
当我将方法更改为:
def country_name
self.country = ISO3166::Country[country]
country.translations[I18n.locale.to_s] || country.name
end
Run Code Online (Sandbox Code Playgroud)
我收到“翻译”这个词的错误。当我从方法中删除“.translations”时,出现“name”这个词的错误。
我正在失去我的弹珠试图弄清楚这一点。
另一种尝试
我尝试将国家宝石添加到我的宝石文件(在 country_select 之上)。
当我捆绑这个 gem 时,没有任何变化。同样的问题。
另一种尝试
再试一次(因为我最初定义了方法),但安装了国家 gem(在 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)
我收到此错误:“开曼群岛”的未定义方法`translations':字符串
这与我最初开始遇到的问题相同,所以我认为添加国家宝石并没有帮助推进解决方案。
小智 5
这段代码对我来说效果很好。我的 User 表中有country列。在我的模型上:-
def country_name
country = self.country
ISO3166::Country[country]
end
Run Code Online (Sandbox Code Playgroud)
形式:-
<%= form_for @user do |f| %>
<%= country_select("user", "country") %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2496 次 |
| 最近记录: |