我正在尝试使用Rails和Simple Form创建一个带有country-select的应用程序。
我收到的错误是:
wrong number of arguments (4 for 0)
我正在使用我找到的示例,对于国家选择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,
除了建议的国家/地区代码之外,我无法计算其他任何内容,但我删除了其中一个以尝试,但我得到了同样的错误。
当我尝试:
<%= 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>
当我尝试按照示例应用程序以及指导样式时,我会收到无法识别方法的错误。
答案 0 :(得分:8)
我终于有了这个工作。我删除了country-select gem(建议使用简单形式)并将其替换为country_select gem。此gem的wiki不使用与wiki上链接的示例应用程序匹配的语法。
这条线最终为我工作:
<%= par.input :participant_country, priority: ["AU", "NZ", "GB", "US"], label: false %>
答案 1 :(得分:2)
将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自述文件中的Wrapping Rails表单助手部分。
答案 2 :(得分:1)
应该是:
<%= 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
答案 3 :(得分:1)
尝试以下代码
<%= par.input :participation_country, as: :country, { priority_countries: ["AU", "GB", "NZ", "US"], selected: :participation_country }, { label: false, prompt: "Select participation country" } %>
参考: 请参阅从country_select提供其他html选项部分。