Format helper to turn tuple into image string

I’m creating an application based on the card game, “War”. I have a game struct with a virtual field of users_cards, which contain a list of shuffled cards in the form of tuples (i.e. {2, :hearts} ).

How can i create a view helper function to translate the tuple into a card image, in this case, “/images/playing_cards/2_of_hearts.png”?

Perhaps I misunderstand your question but in case I don’t you can use string interpolation to easily create this:

def format_card_link({num, suit}) do
  "/images/playing_cards/#{num}_of_#{suit}.png"
end
2 Likes

That was the right way to do it. I was trying to use pattern matching and made it wayyy more confusing than I needed to. So, now I do have a list of the users cards but I think my approach is wrong.

I’m using this

<%= for card <- @changeset.user_cards do %>
  <%= img_tag render_card(card) %>
  <% end %>

to bring in all of the users cards into the view. But what I really should be doing is storing the cards in a list and then running through them one at a time, comparing each card with a card from the computers hand.