RoR select_tag 默认值 &选项 [英] RoR select_tag default value & options

查看:41
本文介绍了RoR select_tag 默认值 &选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 select_tag 设置默认值,以及如何在页面加载时保持选项打开?

解决方案

如果你使用 select_tag 没有任何其他帮助,那么你可以在 html 中进行:

select_tag "whatever", ""

或者使用options_for_select:

select_tag "whatever", options_for_select([ "VISA", "MasterCard" ], "MasterCard")

或者使用options_from_collection_for_select:

select_tag [SELECT_FIELD_NAME], options_from_collection_for_select([YOUR_COLLECTION], [NAME_OF_ATTRIBUTE_TO_SEND], [NAME_OF_ATTRIBUTE_SEEN_BY_USER], [DEFAULT_VALUE])

示例:

select_tag "people", options_from_collection_for_select(@people, 'id', 'name', '1')

示例来自 select_tag 文档options_for_select doc 和来自 options_from_collection_for_select 文档.>

How can I set a default value using select_tag, and how can I keep the options open on page load?

解决方案

If you are using select_tag without any other helper, then you can do it in html:

select_tag "whatever", "<option>VISA</option><option selected=\"selected\">MasterCard</option>"

Or with options_for_select:

select_tag "whatever", options_for_select([ "VISA", "MasterCard" ], "MasterCard")

Or with options_from_collection_for_select:

select_tag [SELECT_FIELD_NAME], options_from_collection_for_select([YOUR_COLLECTION], [NAME_OF_ATTRIBUTE_TO_SEND], [NAME_OF_ATTRIBUTE_SEEN_BY_USER], [DEFAULT_VALUE])

Example:

select_tag "people", options_from_collection_for_select(@people, 'id', 'name', '1')

Examples are from select_tag doc, options_for_select doc and from options_from_collection_for_select doc.

这篇关于RoR select_tag 默认值 &amp;选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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