options_from_collection_for_select -nomethoderror或grighterror-如何添加正确的参数?

发布于 2025-01-28 15:18:01 字数 763 浏览 4 评论 0原文

Rails Console我编写类似的内容时: city.human_attribute_name(“模型”)它的工作正常,返回值,但是当我尝试在.haml中使用它时有错误。

= select_tag("select_tag", options_from_collection_for_select(@searchesForSelectTag, "name", 'human_attribute_name("model")', params[:select_tag]), {:include_blank => ' '})

nomethoderror:未定义的方法`human_attribute_name(“ model”)'for#< class:0x00007fdb90dddd1198>你的意思? human_attribute_name

@searchesforselecttag - 这是类名称的数组(例如用户,城市,汽车...)。

当我只尝试这样的尝试时:“ human_attribute_name”我有参数: 错误的参数数(给定0,预期1..2)

如何将参数“模型”添加到方法humy_attribute_name_name in options_from_collection_for_select中?

In Rails console when I write Something like:
City.human_attribute_name("model") it's work correctly, a return value, but when I try to use it in .haml in Options_from_collection_for_select I have error.

= select_tag("select_tag", options_from_collection_for_select(@searchesForSelectTag, "name", 'human_attribute_name("model")', params[:select_tag]), {:include_blank => ' '})

NoMethodError: undefined method `human_attribute_name("model")' for #<Class:0x00007fdb90dd1198> Did you mean? human_attribute_name

@searchesForSelectTag - this is array of classes name (eg. User, City, Car...).

When I try like only that : "human_attribute_name" I have ArgumentError:
wrong number of arguments (given 0, expected 1..2)

How can I add argument "model" to method human_attribute_name in Options_from_collection_for_select ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2025-02-04 15:18:01

您可以在类上传递一个方法调用,而不是字符串:

= select_tag("select_tag", options_from_collection_for_select(@searchesForSelectTag, "name", City.human_attribute_name("model"), params[:select_tag]), {:include_blank => ' '})

更新

我找到了另一个带有动态类的解决方案。您可以尝试对此进行一些尝试。

<%= select_tag("select_tag", options_for_select(@searchesForSelectTag.map { |item| [item.name, item.class.human_attribute_name("model")] }, params[:select_tag]), {:include_blank => ' '}) %>

Instead of a string, you can pass a method call on a class:

= select_tag("select_tag", options_from_collection_for_select(@searchesForSelectTag, "name", City.human_attribute_name("model"), params[:select_tag]), {:include_blank => ' '})

Update

I found another solution with dynamic classes. You can try to experiment something with this.

<%= select_tag("select_tag", options_for_select(@searchesForSelectTag.map { |item| [item.name, item.class.human_attribute_name("model")] }, params[:select_tag]), {:include_blank => ' '}) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文