【问题标题】:Displaying countries using country_select gem in Rails 4在 Rails 4 中使用 country_select gem 显示国家
【发布时间】:2015-02-01 12:47:20
【问题描述】:

我正在尝试在 Rails 4 应用程序中实现 country_select gem (gem 'country_select','~> 2.1.0'),但没有成功。这是我表单中的代码。我已经从堆栈上的类似问题尝试了几种排列但没有成功。我不明白为什么我会收到错误 -

"ActionView::Helpers::FormBuilder:0x007fafb5878c48 的未定义方法 `input'>"

请记住,我是 Rails 初学者。

    <%= form_for(@user, html: { multipart: true }) do |f| %>

    <%= f.label :country %><br>
    <%= f.input :country, as: :country %>

    <%= f.submit "Update my account", class: "btn btn-primary" %>
    <% end %>

我也试过了

    <%= f.label :country %>
    <%= f.country_select :country  %>

来自文档。 “国家”是我的用户属性之一。

欢迎任何关于新手问题的指导。

【问题讨论】:

  • 将 :country 添加到用户强参数

标签: ruby-on-rails select country


【解决方案1】:

我刚刚在同一个问题上花了一个小时,并且对 github 文档的简洁感到恼火。不过,这对我有用,听起来像你需要的:

<%= f.country_select(:country, {selected: @user.country}, {class: "form-control"}) %>

我无法让第二个参数(请参阅 https://github.com/stefanpenner/country_select)工作 - 每次我尝试在 {selected...} 之前添加任何其他内容时都会出错。

【讨论】:

  • 嗨,MSC。我在我的代码中尝试了你的建议(使用简单的表单,country_select 和国家 gems),错误得到这个错误:undefined local variable or method `country_code' for #<0x007ff8ef158e58>
【解决方案2】:
<%= f.country_select :country, {priority_countries: ["SG", "US"], include_blank: "select country"}, class: 'form-control' %>

这对我有用,第一个:countryuser 的属性,country_select gem 的所有选项将作为下一个参数,最后一个参数将采用类名和其他格式特征。

如果参数包含单个字段,则可以带或不带括号传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多