如何让 country_select gem 和 simple_form 从头开始​​工作? [英] How do I get the country_select gem and simple_form to work from scratch?

查看:45
本文介绍了如何让 country_select gem 和 simple_form 从头开始​​工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想要一个简单的国家/地区下拉列表,优先考虑我的目的(即包括所选国家/地区内的州).

I just want a simple country dropdown, prioritized for my purposes (i.e. to include the states within the country selected).

其实我只想要一两个国家.

In fact, I only want 1 or 2 countries.

我正在使用 Simple_Form 并且他们的文档说为了访问这个:

I am using Simple_Form and their documentation say that in order to get access to this:

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

我只需要将它包含在我的 Gemfile 中:

I just need to include this in my Gemfile:

gem 'country_select'

但我很困惑...我不必运行迁移来在我的 Post 模型上存储国家选择 - 这是我将修改我的 _form 的模型.html.erb for?

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?

当我只是这样做时:

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

我收到一个邮政编码无国家方法错误.我不得不将此添加到我的 Post.rb:

I got a no country method on Post error. I had to add this to my Post.rb:

attr_accessor :country

如果我没记错的话,我应该不会因为强参数而在 Rails 4 中这样做,对吗?

Which if my memory serves me correctly, I should no longer have to do in Rails 4 because of Strong Parameters, right?

那行得通,但现在 collection: 规范不起作用.我仍然看到一长串国家/地区.

That works, but now the collection: specification doesn't work. I still see a long list of countries.

请记住,我不必运行任何迁移来修改我的 Post.rb 模型.我不知道要添加哪些列.我是将 country, state, city 添加到我的 Post 模型中,还是创建一个名为 Country 的新模型?

Keep in mind I have not had to run any migrations to modify my Post.rb model. 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?

关于 gem 中的国家(国家/地区代码、城市、州等)的所有这些信息是否都是通过 Gem 中的 YAML 文件加载的?

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?

我不知道,而且文档非常少.

I have no clue and the documentation is surprisingly sparse.

所以我的问题很简单:

  1. 如何让这个 simple_form 集合起作用.
  2. 如何让国家的其他属性出现在表单中(例如州和/或城市)?
  3. 我是否必须在某个表中提供这些其他数据?
  4. 我如何关联国家和地区?用户使用我的 Post 记录选择的城市/州?
  1. How do I get this simple_form collection to work.
  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?

谢谢.

推荐答案

解决方案 1:

simple_form 的文档用于 country_select 1.x.要按照文档使用 simple_form,请在您的 gemfile 中限制您的 country_select 版本:

Solution 1:

The documentation for simple_form is for use with country_select 1.x. To use simple_form as documented, restrict your version of country_select in your gemfile:

gem 'country_select', '~> 1.0'

然后捆绑更新 country_select 以降级.

如果您想改用 country_select 2.x,您将使用国家/地区代码,而不是国家/地区名称.您的表单助手看起来与 simple_form 文档略有不同:

If you want to use country_select 2.x instead, you'll be working with country codes, not country names. Your form helper will look a little different from the simple_form docs:

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

对于任一解决方案

确实需要在您的 Post 模型中添加一列来存储用户在您的数据库中选择的国家/地区.数据类型将为字符串/varchar.您可以使用以下内容生成迁移:

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. You can generate the migration with something like:

rails generate migration AddCountryCodeToPost country_code:string

country_select 只知道国家.

不是城市或州.

如果您希望用户提供州或城市,您可以让他们填写文本字段并将他们提供的任何内容存储为字符串(如上所述,您将这些列添加到您的模型中),或者构建您自己的选择/自动完成系统.有一个 state_select gem,但您需要使用 javascript 或流水线形式将其与 country_select 手动集成.我不知道有任何按州/国家/地区提供城市名称数据源的 gem,但很可能有一些.

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. 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.

有关 country_select 1.x 和 country_select 2.x 之间差异的更多信息,请参阅 升级指南.

For more on the differences between country_select 1.x and country_select 2.x, see the upgrade guide.

这篇关于如何让 country_select gem 和 simple_form 从头开始​​工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆