简体   繁体   English

如何从头开始使用country_select gem和simple_form?

[英]How do I get the country_select gem and simple_form to work from scratch?

I just want a simple country dropdown, prioritized for my purposes (ie to include the states within the country selected). 我只想要一个简单的国家/地区下拉列表,出于我的目的(即将所选国家/地区中的州包括在内)优先考虑。

In fact, I only want 1 or 2 countries. 实际上,我只想要1或2个国家。

I am using Simple_Form and their documentation say that in order to get access to this: 我正在使用Simple_Form,他们的文档说是为了获得对此的访问权限:

f.input :shipping_country, priority: [ "Brazil" ], collection: [ "Australia", "Brazil", "New Zealand"]

I just need to include this in my Gemfile: 我只需要在我的Gemfile中包含它:

gem 'country_select'

But I am confused...I don't have to run a migration to store the country selection on my Post model - which is the model I will be modifying my _form.html.erb for? 但是我很困惑...我不必运行迁移就可以将国家/地区选择存储在我的Post模型中-我_form.html.erb针对哪个模型修改_form.html.erb

When I simply do: 当我简单地做:

<%= f.input :country, as: :country, collection: [ "Australia", "Brazil", "New Zealand"] %>

I got a no country method on Post error. no country method on Post错误no country method on Post遇到了一个no country method on Post I had to add this to my Post.rb : 我必须将其添加到我的Post.rb

attr_accessor :country

Which if my memory serves me correctly, I should no longer have to do in Rails 4 because of Strong Parameters, right? 如果我的记忆正确地为我服务,那么由于“强参数”而不再需要在Rails 4中做对了吗?

That works, but now the collection: specification doesn't work. 那行得通,但是现在collection:规范不起作用。 I still see a long list of countries. 我仍然看到一长串国家。

Keep in mind I have not had to run any migrations to modify my Post.rb model. 请记住,我不必运行任何迁移即可修改Post.rb模型。 I don't know what columns to add. 我不知道要添加哪些列。 Do I add say country, state, city to my Post model or do I create a new model called Country ? 我可以在“ Post模型中添加country, state, city ,还是要创建一个名为“ Country的新模型?

Is all of this information about the countries (country code, cities, states, etc.) in the gem being loaded via a YAML file in the Gem? 是否通过宝石中的YAML文件加载了有关宝石中的国家(国家代码,城市,州等)的所有信息?

I have no clue and the documentation is surprisingly sparse. 我没有任何线索,文档稀疏。

So my questions are simply these: 所以我的问题很简单:

  1. How do I get this simple_form collection to work. 如何使这个simple_form集合起作用。
  2. How do I get other attributes of the country to appear in the form (like state and/or city)? 如何获得国家/地区的其他属性以表格形式显示(例如州和/或城市)?
  3. Do I have to supply this other data in a table somewhere? 我是否必须在某个地方的表中提供其他数据?
  4. How do I associate the country & city/state selected by the user with my Post record? 如何将用户选择的国家和城市/州与我的Post记录相关联?

Thanks. 谢谢。

Solution 1: 解决方案1:

The documentation for simple_form is for use with country_select 1.x. simple_form的文档与country_select 1.x一起使用。 To use simple_form as documented, restrict your version of country_select in your gemfile: 要按照记录使用simple_form,请在gemfile中限制country_select的版本:

gem 'country_select', '~> 1.0'

and then bundle update country_select to downgrade. 然后bundle update country_select降级。

Solution 2: 解决方案2:

If you want to use country_select 2.x instead, you'll be working with country codes, not country names. 如果要改为使用country_select 2.x,则将使用国家/地区代码,而不是国家/地区名称。 Your form helper will look a little different from the simple_form docs: 您的表单帮助器看起来与simple_form文档略有不同:

<%= f.input :country_code, as: :country, priority: ['BR'] %>

For either solution 对于任何一种解决方案

You do need to add a column to your Post model to store the country the user has selected in your database. 确实需要在“帖子”模型中添加一列,以存储用户在数据库中选择的国家/地区。 The data type will be a string/varchar. 数据类型将为字符串/ varchar。 You can generate the migration with something like: 您可以通过以下方式生成迁移:

rails generate migration AddCountryCodeToPost country_code:string

country_select only knows about countries. country_select只知道国家。

Not cities or states. 不是城市或州。

If you want users to supply states or cities, you'll either let them fill in text fields and store whatever they supply as strings (you'll add columns for these to your model as above), or build your own select/autocomplete system. 如果您希望用户提供州或城市,则可以让他们填写文本字段并以字符串形式存储提供的任何内容(您将在上面为模型添加这些列),或者构建自己的选择/自动完成系统。 There's a state_select gem, but you'll need to manually integrate it with country_select using javascript or a pipelined form. 有一个state_select宝石,但是您需要使用javascript或管道形式将其与country_select手动集成。 I'm not aware of any gems that provide data sources for city names by state/country, but there may very well be some out there. 我不知道有哪个宝石可以提供按州/国家/地区提供的城市名称数据源,但很可能还有一些宝石。

Note 注意

For more on the differences between country_select 1.x and country_select 2.x, see the upgrade guide . 有关country_select 1.x和country_select 2.x之间的区别的更多信息,请参阅升级指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM