如何在 Rails 中为 collection_select 设置 HTML 选项? [英] How do I set the HTML options for collection_select in Rails?

查看:30
本文介绍了如何在 Rails 中为 collection_select 设置 HTML 选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到将类添加到由 Rails 生成的选择标记的语法 collection_select.有什么帮助吗?

I can't seem to find the syntax to add a class to a select tag generated by Rails collection_select. Some help?

推荐答案

许多 Rails 助手接受多个哈希参数.第一个通常是控制助手本身的选项,第二个是 html_options,您可以在其中指定自定义 id、类等.

Many Rails helpers take multiple hash arguments. The first is usually the options to control the helper itself, and the second is the html_options where you specifiy custom ids, classes etc.

方法定义如下所示:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

您会注意到参数列表中的多个= {}".要使用它,您要指定的第一组选项实际上必须括在大括号中:

You'll notice the multiple '= {}' in the params list. To use this, the first set of options that you would specify must actually be enclosed in braces:

collection_select(:user, :title, UserTitle.all, :id, :name, {:prompt=>true}, {:class=>'my-custom-class'})

如果除了 html 类之外没有任何选项可以指定,那么只需放置一个空的哈希占位符:

If you don't have any options to specify besides the html class, then just put an empty hash placeholder:

collection_select(:user, :title, UserTitle.all, :id, :name, {}, {:class=>'my-custom-class'})

其他 API 文档位于:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

Additional API documentation is available at: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

这篇关于如何在 Rails 中为 collection_select 设置 HTML 选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆