Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

ruby on rails - RoR select_tag default value & options

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...