79752253

Date: 2025-09-01 07:32:34
Score: 1
Natty:
Report link

Main Problems: You're checking for params.key("king") but should check for the value

You're using @top_discard which contains the card object, not the code

The API calls for replacing the king aren't being executed properly

You need to handle the king replacement as a separate operation

Here's the corrected code: ruby

get("/discard") do
  # ... [previous code remains the same until the king section] ...

  ################################################### start of change suit with king
  @is_king = false
  if @top_discard.fetch("value") == "KING"
    @is_king = true
  end

  # Check if king parameter is present and not empty
  @in_king = false
  if params.key?("king") && !params["king"].empty?
    king_code = params["king"]
    @in_king = true

    # Remove the current king from discard pile
    deck = cookies[:deck_id]
    pile_name = "discard"
    
    # Draw (remove) the current king from discard pile
    remove_king_url = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/draw/?cards=" + @top_discard["code"]
    HTTP.get(remove_king_url)

    # Add the new king to discard pile
    add_new_king_url = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/add/?cards=" + king_code
    HTTP.get(add_new_king_url)

    # Refresh the discard pile data
    discard_list = "https://deckofcardsapi.com/api/deck/" + deck + "/pile/" + pile_name + "/list/"
    @discard_res = api_response(discard_list, "piles").fetch(pile_name).fetch("cards")
    
    # Update top discard card
    @top_discard = @discard_res.last
    @discard_arr = @discard_res.map { |card| card.fetch("image") }
  end

  # Only load kings selection if top card is king AND we haven't already chosen one
  if @is_king && !@in_king
    new_deck = "https://deckofcardsapi.com/api/deck/new/?cards=KS,KC,KH,KD"
    resp = HTTP.get(new_deck)
    raw_response = resp.to_s
    parsed_response = JSON.parse(raw_response)
    @kings_deck_id = parsed_response.fetch("deck_id")

    king_draw = "https://deckofcardsapi.com/api/deck/" + @kings_deck_id + "/draw/?count=4"
    @cards_to_add = api_response(king_draw, "cards")

    king_add = []
    @cards_to_add.each do |c|
      king_add.push(c.fetch("code"))
    end
    
    pile_name = "kings"
    cards = king_add.join(",")
    
    pile = "https://deckofcardsapi.com/api/deck/" + @kings_deck_id + "/pile/" + pile_name + "/add/?cards=" + cards
    resp = HTTP.get(pile)

    pile_list = "https://deckofcardsapi.com/api/deck/" + @kings_deck_id + "/pile/" + pile_name + "/list/"
    @kings = api_response(pile_list, "piles").fetch("kings").fetch("cards")

    @king_arr = []
    @king_codes = []
    @kings.each do |c|
      @king_arr.push(c.fetch("image"))
      @king_codes.push(c.fetch("code"))
    end
  end

  erb(:discard)
end

Key Changes: Fixed parameter checking: Use params.key?("king") && !params["king"].empty?

Use card code instead of object: @top_discard["code"] instead of @top_discard

Proper API sequence: Remove old king → Add new king → Refresh data

Conditional king loading: Only load king options if needed and not already processed

In your ERB template, make sure you have:

erb
<% if @is_king && !@in_king %>
  <form action="/discard" method="get">
    <h3>Choose a suit for the King:</h3>
    <% @kings.each do |king| %>
      <label>
        <input type="radio" name="king" value="<%= king['code'] %>">
        <img src="<%= king['image'] %>" height="100">
      </label>
    <% end %>
    <button type="submit">Change Suit</button>
  </form>
<% end %>
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @top_discard
  • User mentioned (0): @top_discard
  • Low reputation (1):
Posted by: holk alOmari