views:

99

answers:

2

In Rails erb, am using the snippet to show visiting team in a tournament match. How do I get to initially show the current visiting_team? What am I doing wrong?

<%= f.select(:visiting_team_id, Team.all.collect{|t| [t.name, t.id] }) %>
A: 

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

Gareth
It is correct; I checked by adding a <%= h @match.visiting_team_id%> and it shows the correct id.
Ram
What else should I check ?
Ram
A: 

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}) %>
Ram
or is there a better alternative ?
Ram