Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 RubyonRails web表单的国家/地区选择_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 RubyonRails web表单的国家/地区选择

Ruby on rails 3 RubyonRails web表单的国家/地区选择,ruby-on-rails-3,Ruby On Rails 3,我想在Rails web表单中包含国家选择框。如何在RubyonRails中实现这一点?我的表格是这样的 我可以包括哪些内容来代替国家/地区列表?过去用于提供国家/地区选择功能的Rails。该功能已从core中提取出来,现在已打包到插件中 使用插件启用该功能。我的意见是在数据库中为国家列表添加种子 创建具有字段“name”的国家/地区模型 在app/models/country.rb中 attr_accessible :name 从YAML文件中加载国家列表并将其种子添加到数据库中 在conf

我想在Rails web表单中包含国家选择框。如何在RubyonRails中实现这一点?我的表格是这样的


我可以包括哪些内容来代替国家/地区列表?

过去用于提供国家/地区选择功能的Rails。该功能已从core中提取出来,现在已打包到插件中


使用插件启用该功能。

我的意见是在数据库中为国家列表添加种子

创建具有字段“name”的国家/地区模型

在app/models/country.rb中

attr_accessible :name
从YAML文件中加载国家列表并将其种子添加到数据库中

在config/country.yml中

-
 name: India
 name: Pakistan
 name: Cuba
 #add required country name
在db/seed.rb中

COUNTRIES  = YAML.load_file(Rails.root.join('config/country.yml'))
COUNTRIES.each do |country|
  Country.create(country)
end

在用户模型中添加国家/地区id字段编写迁移以添加字段

在app/models/user.rb中

class User < ActiveRecord::Base
 #associate user with country
 belongs_to :country
end
在新用户表单中添加以下代码

 <%= f.select :country_id, Country.all.collect { |country| [country.name, country.id] },
  { :prompt => "Select Country" } %>

它确实适用于country_select gem

允许您执行相同的操作,但使用标准的两个字母代码,而不是国家的具体英语单词:此示例有一个包含代码的字段country,选择框在顶部添加首选国家字段,以本地化形式表示瑞典。瑞典的关键是SEf、 country_code_select:country,[[I18n.t:SE,:scope=>:countries,'SE']]嘿,这是一块很棒的宝石,对我来说很有用,谢谢。
class User < ActiveRecord::Base
 #associate user with country
 belongs_to :country
end
 <%= f.select :country_id, Country.all.collect { |country| [country.name, country.id] },
  { :prompt => "Select Country" } %>
<%= f.country_select :country, ["United States"] %>