Krishna Kumar
Krishna Kumar

Reputation: 511

select option tag in ror?

i am new at ror.

option select tag in rails

<%= select_tag(:city_id, '<option value="1">Lisbon</option>,<option value="2">Madrid</option>' %>

produces this html:

<select id="city_id" name="city_id">
    <option value="1">Lisbon</option>
<option value="2">hjkLisbon</option>
</select>

and option select tag

<%= select_tag(:city_id, options_for_select([['Lisbon', 1], ['Madrid', 2]])) %>

produces this html:

<select id="city_id" name="city_id">
<option value="1">Lisbon</option>
<option value="2">Madrid</option>
</select>

for second option select tag i am getting the dropdown with options but in first one i am not getting it, only dropdown is there in first one but no options. can anybody please explain me why it is so?

Upvotes: 1

Views: 5273

Answers (3)

soft
soft

Reputation: 1

if you really want to keep selected the country name on edit page then..

<%= f.select :country, (country_name), :selected => @model.country %>

Where country_name is a method in helper.

Upvotes: -1

Salil
Salil

Reputation: 47482

Ref this

Change

<%= select_tag(:city_id, '<option value="1">Lisbon</option>,<option value="2">Madrid</option>' %>

To

<%= select_tag(:city_id, '<option value="1">Lisbon</option>,<option value="2">Madrid</option>') %>

You are not closing the select tag i.e. ) is missing

Upvotes: 0

Andreas Lyngstad
Andreas Lyngstad

Reputation: 4927

If you only have these two cities you can use normal html

<select id="city_id" name="city_id">
<option value="1">Lisbon</option>
<option value="2">Madrid</option>
</select>

If you fetch the cities from you database you can do

select_tag ‘city_id’, options_for_select(@cities.collect{ |u| [u.name, u.id] })

This would generate something like:

<select id="city_id" name="city_id">
  <option value="1">Lisbon</option>
  <option value="2">Madrid</option>
</select>

Do not be afraid to use html. The most simple solution is the best solution.

Upvotes: 2

Related Questions