Rails 简单的国家/地区 Select

Rails Simple Form Country Select

我正在尝试使用 Rails 和带有国家/地区 select.

的简单表单制作一个应用程序

我收到一条错误消息:

wrong number of arguments (4 for 0)

我正在使用我在国家/地区 select gem 文档页面上找到的简单表格示例。我不明白这是怎么回事。

在我的模型中我有:

     geocoded_by :address
  after_validation :geocode, if: ->(obj){ @obj.address.present? and @obj.address_changed? }


  def address
    [city, state, participation_country].compact.join(', ')
  end

在我的表格中我有:

 <%= f.simple_fields_for :scope do |participants_s| %>
      <%= participants_s.simple_fields_for :participant do |par| %>
<%= par.select :participation_country,
                           priority: ["AU", "GB", "NZ", "US"],
                           selected: :participation_country,
                          label: false,
                          prompt: "Select participation country" %>

我在使用

时遇到同样的错误
<%= par.input :participation_country,

在我看来我有:

<%= @project.scope.participant.address %>

谁能看出我在尝试设置时做错了什么?错误消息表明有问题的行是:

<%= par.select :participation_country,

除了建议的国家/地区代码外,我不能数出 4 个,尽管我删除了其中一个来尝试,但我得到了同样的错误。

当我尝试时:

          <%= par.country_select("participation_country", {:prompt => "Choose Country"})%>

我得到同样的错误:参数数量错误(4 代表 0)

我也试过删除 country-select gem 并改为安装 country_select gem。使用简单形式的 country_select gem 的指南有这个例子:

country_select("user", "country", only: ["GB", "FR", "DE"])

然而,简单表单的示例应用程序显示为:

  <fieldset>
    <legend>With Only Chosen Countries</legend>
    <%= f.input :country_code, only: ["LV","SG"] %>
    <code>f.input :country_code, only: ["LV","SG"]</code>

当我尝试按照示例应用程序和指南样式进行操作时,我遇到无法识别方法的错误。

应该是:

<%= par.input :participation_country, as: :country,
                           priority_countries: ["AU", "GB", "NZ", "US"],
                           label: false,
                           prompt: "Select participation country" %>

您无需指定 selected 选项,表单生成器会为您完成。

参考文献:

https://github.com/stefanpenner/country_select#usage

How to list all countries in select using country_select gem with simple_form

试试下面的代码

<%= par.input :participation_country, as: :country, { priority_countries: ["AU", "GB", "NZ", "US"], selected: :participation_country }, { label: false, prompt: "Select participation country" } %>

参考: 请参见 country_select.

中提供额外的 html 选项部分

将par.select更改为f.input

<%= f.simple_fields_for :scope do |participants_s| %>
      <%= participants_s.simple_fields_for :participant do |par| %>
        <%= f.input :participation_country, as: :country, { priority_countries: ["AU", "GB", "NZ", "US"], selected: :participation_country }, { label: false, prompt: "Select participation country" } %>

参考:

请参阅 simple_form 的自述文件中的包装 Rails 表单助手部分。

我终于搞定了。我删除了 country-select gem(建议与简单形式一起使用)并将其替换为 country_select gem。此 gem 的 wiki 未使用与 wiki 上链接的示例应用程序相匹配的语法。

这条线最终对我有用:

<%= par.input :participant_country, priority: ["AU", "NZ", "GB", "US"], label: false %>