I’m beginning development on a Rails app and before I start heading in the wrong direction, I’d like to see if there is a common way (ie. best practice) to do this. I have the models Game and Player in a has_many :through relationship. And I have these partials to display the items:
views/games/_game.html.erb
<%= content_tag_for :li, game do %>
<span class="name"><%= game.name %></span>
<% if game.players.any? %>
<ul class="player_list">
<%= render :partial => 'players/player', :collection => game.players %>
</ul>
<% end %>
<% end %>
views/players/_player.html.erb
<%= content_tag_for(:li, player) do %>
<span class="name"><%= player.name %></span>
<span class="delete">
<%= link_to "delete", player, :method => :delete, :class => :delete %>
<span>
<% end %>
The problem is that I want players to display differently in different contexts. For example, players should not be “delete-able” from the game show page like they might on the player index. Is it acceptable to create another partial like views/games/_player.html.erb that determines how players are displayed in the context of a game? Or is there a better solution?
When you say:
I believe that is not the purpose of a partial.
It seems you are giving logic to the view, and that is somehow wrong. I believe you could have a method in a helper that would do that decision logic.
So, here is what I think I would do it in the example that you gave: