Rails select helper. How do I select the default value?
In Rails erb, am using the snippet to show visiting team in a tournament match. How do I get to initially show the current visit开发者_如何学运维ing_team? What am I doing wrong?
<%= f.select(:visiting_team_id, Team.all.collect{|t| [t.name, t.id] }) %>
The fix is to explicitly specify the selected option. It now looks likes this and works
<%= f.select(:visiting_team_id, Team.all.collect{|t| [t.name, t.id]}, {:selected => @match.visiting_team_id.to_i}) %>
Just ensure that the object you're passing to form_for
has the correct visiting_team_id
set. Then, so long as there's a team that gets pulled out with that ID, it should work
精彩评论