Victor
Victor

Reputation: 13378

Weird country_options_for_select in Rails

I currently have my index.html.erb showing the following code.

<select name="country">  
<option>All</option>
<%= country_options_for_select('All') %>
</select>

But the result of the page becomes like this in the html source:

<select name="country">  
<option>All</option>
&lt;optionvalue=&quot;Afghanistan&quot;&gt;Afghanistan&lt;/option&gt;&lt;optionvalue=&quot;Aland
Islands&quot;&gt;AlandIslands&lt;/option&gt; ... 
</select>

It should be instead of <option>

What did I do wrong?

Upvotes: 0

Views: 405

Answers (2)

Victor
Victor

Reputation: 13378

In Rails 2.3.14 to Rails 3.1.0, this works:

<%= country_options_for_select.html_safe %>

Upvotes: 1

John Dyer
John Dyer

Reputation: 1211

Try using select_tag instead. It looks a bit cleaner.

<%= select_tag "name", country_options_for_select() %>

The reason the country options are showing up incorrectly is because you are passing 'All' to it. It doesn't need an argument there in your case. Only if you wanted a certain option selected by default.

For example,

<%= select_tag "name", country_options_for_select('Chile') %>

More info on it's use here: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/country_options_for_select

Upvotes: 1

Related Questions