简体   繁体   中英

How can I add default selected Choose a field option to my drop-down in rails?

In my signup I have Choose a Question drop-down:Code below

<%= f.select("question_id", Question.all.collect {|p| [ p.body, p.id ] }])) %>

and I want to get the output like below

<select name="question" id="login_fields_question" class="signup_fields"  >
<option value="choosequestion" selected="selected" style="font-style:italic;">Choose a question ...</option>
<option value="1"> What is your pet name?</option> 
<option value="2">What is the name of your best friend from childhood?</option>
<option value="3">What was the name of your first teacher?</option>
<option value="4">What is the name of your manager at your first job?</option>
<option value="5">What was your first phone number?</option>
<option value="6">What is your vehicle registration number?</option>

With the code f.select I could get all questions from database but How can I add Choose a question ... option to my drop-down? which will be shown on default and could give output as above HTML code

Have you tried to use the :prompt => "Choose a question ..." option?

I hope help you!

Hugs!

另一种选择是:

= f.select "question_id", Question.all.collect {|p| [ p.body, p.id ] }, include_blank: "Choose a question ..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM