Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

How do I set the HTML options for collection_select in Rails?

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.

The method definition looks like this:

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...