New to Ruby and Rails.
I am developing a simple app for where you can register teams, players etc and it looks like this:
Team has_many Players Players belongs_to Team
When I want to show the player in view(normal users):
<%= @player.name %> - <%= playerteam %>
and in the admin view it looks like this:
<% @players.each do |player| %>
<tr>
<td><%= player.id %></td>
<td><%= player.name %></td>
<td><%= playerteam(player) %></td>
<td><%= owner(player) %></td>
</tr>
<% end %>
and the helper method:
def playerteam(player = nil)
if player != nil
if player.team_id == nil
return "No team"
else
@team = Team.find(player.team_id)
return @team.name
end
else
if @player.team_id == nil
return "No team"
else
@team = Team.find(@player.team_id)
return @team.name
end
end
end
It works but it is not pretty or "Ruby Sexy"
At first it was only used from the normal view but then when I wanted to use it from the admin-view also I had to add the parameter with a default value and the extra if-clause.
Are there a better way?