gpt4 book ai didi

ruby - 使用带有 form_tag 且没有模型的 country_select gem

转载 作者:数据小太阳 更新时间:2023-10-29 08:37:23 26 4
gpt4 key购买 nike

我在 Rails 4 的表单标签中使用 country_select gem:

<%= form_tag(@url, method: :post, class: "form-horizontal") do %>
<div class="form-group">
<%= label_tag(:country_code, "Country code") %>
<%= country_select :country_code, @country_code, class: "form-control" %>
</div><% end %>

我收到以下错误。

undefined method `NZ' for "NZ":String

这是一个安静的异常错误,我还没有为 country_select gem 找到它的任何其他实例。 Rails 应用程序没有数据库并且正在与外部数据库通信,因此此应用程序中没有模型。有人建议我使用 Open Struct 来解决这个问题。但是,我想知道是否有人知道更快的修复方法?表单正在获取数据库中的数据!

最佳答案

因为您的应用程序中可能有 country_select gem,所以您也有 countries gem,因为它是 country_select 的依赖项。所以你可以像这样使用 countries gem 来使用普通的 Rails 表单助手 select_tagoptions_for_select :

select_tag :country, options_for_select(ISO3166::Country.all, params[:country])

这是比使用 OpenStruct 执行此操作更好的选择。

有关详细信息和其他可以代替 ISO3166::Country.all 的查找器,请参阅 https://github.com/hexorx/countries

关于ruby - 使用带有 form_tag 且没有模型的 country_select gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com