Manish Shrivastava
Manish Shrivastava

Reputation: 32020

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

Upvotes: 2

Views: 450

Answers (2)

Simon Bagreev
Simon Bagreev

Reputation: 2859

Another option would be:

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

Upvotes: 2

Marcus S&#225;
Marcus S&#225;

Reputation: 608

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

I hope help you!

Hugs!

Upvotes: 4

Related Questions