【问题标题】:Rails Simple_Form: How to show long name of the countryRails Simple_Form:如何显示国家的长名称
【发布时间】:2014-09-18 08:48:04
【问题描述】:

我使用simple_form gem 来选择国家:

= simple_form_for @shop, :url => { :action => "create" }, :html => {:id => 'new_shop' } do |f|
  = f.simple_fields_for :address, :html => {:multipart => true} do |o|
    = o.input :country, :label => "Country"

但国家名称以短格式保存在数据库中(如RUFRAU 等)。

我想知道,如何在视图中显示完整的长名称?谢谢!

【问题讨论】:

  • 看看thisgem?
  • 数据库中有长格式冲突吗?
  • @Nitish,谢谢,我去看看!
  • @Зелёный,不,它在数据库中是短格式。
  • 你发现了吗?我和你有同样的问题

标签: ruby-on-rails format simple-form country


【解决方案1】:

实际上在数据库中保存国家代码(不是长名称)是个好主意,因为 I18n。有了代码以后可以得到如下名称:

class User < ActiveRecord::Base
  # Assuming country_select is used with User attribute `country_code`
  # This will attempt to translate the country name and use the default
  # (usually English) name if no translation is available
  def country_name
    country = ISO3166::Country[country_code]
    country.translations[I18n.locale.to_s] || country.name
  end
end

检查:country_select: Getting the Country Name from the countries gem

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多