开发者

Rails: how does member of collection get called in option_groups_from_collection_for_select

Im about to figure out how to call a function with one member of the collection I want to thisplay in a option_groups_from_collection_for_select.

The sample is as following:

option_groups_from_collection_for_select(@categories, :children, :name, :id, :name, 3)

In my code I need t开发者_开发技巧o replace the :name tag with a function that calculates the right translation. When I write this function in the model there comes a error that he does not find this method name. If i put it in to the helper controller, I dont have the information of which categorie should get translated...

The manual writes about that label method:

group_label_method: The name of a method which, when called on a member of collection, returns a string to be used as the label attribute for its <optgroup> tag.

Where do I have to write this?

Thanks Markus


I assume you are calling the function like this:

option_groups_from_collection_for_select(@categories, :children, my_translate_function, :id, :name, 3)

This doesn't work because "my_translate_function" is not defined.

you have to put an colon in front of the function name like this:

option_groups_from_collection_for_select(@categories, :children, :my_translate_function, :id, :name, 3)

rails will then use the send method to call "my_translate_function" on your object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜