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
746 views
in Technique[技术] by (71.8m points)

ruby on rails - Include blank for first item in select list in options_for_select tag

I tried :include_blank => true, but it didn't work.

<select>
    <%= options_for_select Model.all.collect{|mt| [mt.name, mt.id]} %>
</select>

If I need to add it to the collection, how would you do that?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I think you want this format:

select("model_name", "model_id", Model.all.collect {|mt| [ mt.name, mt.id ] }, {:include_blank => 'name of your blank prompt'})

BTW: was assuming Modle was suppose to be Model. To use using collection_select:

collection_select(:model, :model_id, Model.all, :id, :name, :prompt => true)

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

...