"Phoenix in Action" - the book exercises

“Phoenix in Action” is not new, but I can’t find solutions to those tasks included in this book (and I want to do them as part of the learning process as I’m new to programming):

The exercises:

  • Only allow bids that have a higher amount than the current high bid
  • Don’t allow bids on items after the item’s end_at date and time have passed
  • Only allow the creation of an item if a user is logged in
  • Associate newly created items with the logged-in user
    I tried it here without luck: Passing current_user.id into form
  • Display the bid date time in the user’s time zone (it’s currently in UTC)

Code:

Has someone finished it? Any help appreciated!

I’m trying to implement “delete item” action.

Auction.delete_item

  def delete_item(id) do
  get_item(id)
  |> @repo.delete()
  end

AuctionWeb.ItemController (need some work)

  def delete(_conn, %{"id" => id}) do
   Auction.delete_item(id)
  end

Routes

    page_path  GET     /                     AuctionWeb.PageController :index
    item_path  GET     /items                AuctionWeb.ItemController :index
    item_path  GET     /items/:id/edit       AuctionWeb.ItemController :edit
    item_path  GET     /items/new            AuctionWeb.ItemController :new
    item_path  GET     /items/:id            AuctionWeb.ItemController :show
    item_path  POST    /items                AuctionWeb.ItemController :create
    item_path  PATCH   /items/:id            AuctionWeb.ItemController :update
               PUT     /items/:id            AuctionWeb.ItemController :update
    item_path  DELETE  /items/:id            AuctionWeb.ItemController :delete

index.html.eex

<ul>
  <%= link to: Routes.item_path(@conn, :new) do %>
    add new
  <% end %>
  <%= for item <- @items do %>
  <li><strong><%= link(item.title, to: Routes.item_path(@conn, :show, item)) %></strong>:
  <%= item.description %> <%= item.user.username %>
    <%= if @current_user == item.user do %>
    | <%= link "Edit", to: Routes.item_path(@conn, :edit, item) %>
      <%= link "Delete", to: Routes.item_path(@conn, :delete, item) %>
    <% end %>
</li>
  <% end %>
</ul>

The problem is - that link is not working. It just shows me the item.

What am I doing wrong?

I’m not sure I’m following the issue you’re having exactly, but it looks as you might need to pass the request :method option to the link function: https://hexdocs.pm/phoenix_html/Phoenix.HTML.Link.html#link/2-options

I would also advise you join the slack channel to have easy access to help as you go forward: elixir-lang.slack.com (assuming you’re not already there, but I think it’s easier to troubleshoot these kinds of things there)

2 Likes

As mentionned, it should be…

<%= link "Delete", to: Routes.item_path(@conn, :delete, item), method: :delete %>

# or, with confirmation

<%= link "Delete", to: Routes.item_path(@conn, :delete, item), method: :delete,  
    data: [confirm: gettext("Are you sure?")] %>
1 Like

It’s working! Thanks!

but… if there are “bids” attached to the item, I get this error:

So I assume my function item_delete have to check if there are bids, and then delete those bids, later on → delete the item. Am I right?

You need to set on_delete option on the association to manage this…

1 Like

Ok, thanks!