Domanda

I'm trying to get the User's Selection to stick in the options_for_select.

Heres my code:

<div class="form-group">
  <%= f.label :team_for, "I'm for" %> <br>
  <%= f.select :team_for, options_for_select(User::TEAMFOR), :prompt => "Please Select" %>
</div>

How can i set this up?

My HTML output:

<div class="form-group">
  <label for="user_team_for">I'm for</label> <br>
  <select id="user_team_for" name="user[team_for]">

  <option value="">Please Select</option>
  <option value="Team Druid">Team 1</option>
  <option value="Team Hunter">Team 2</option>
  <option value="Team Mage">Team 3</option>
  <option value="Team Paladin">Team 4</option>
  <option value="Team Priest">Team 5</option>
  <option value="Team Rogue">Team 6</option>
  <option value="Team Shaman">Team 7</option>
  <option value="Team Warrior">Team 8</option></select>
</div>

If i select something, it gets into the Database and is shown correctly, but on the Edit Page the selection is not shown, instead always "Team 1" is set as selected.

I need the selected value to be shown.

È stato utile?

Soluzione

The 2nd param to options_for_select is the selected value: http://guides.rubyonrails.org/form_helpers.html#option-tags-from-a-collection-of-arbitrary-objects

The second argument to options_for_select must be exactly equal to the desired internal value. In particular if the value is the integer 2 you cannot pass "2" to options_for_select — you must pass 2. Be aware of values extracted from the params hash as they are all strings.

Try this:

<%= f.select :team_for, options_for_select(User::TEAMFOR, @user.team_for), :prompt => "Please Select" %>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top