'Options_from_collection_for_select - NoMethodError Or ArgumentError - how add properly argument?

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 ?



Solution 1:[1]

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 => ' '}) %>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1