I’m new to Elixir & Phoenix and I’ve been struggling to find a solution to the following problem.
I’m creating an inventory app to keep track of my Magic: The Gathering cards. I used a generator to get started, creating everything I need to create, edit and view cards.
I want to add a search feature where I can enter a set id and a card number. Using the code to add new card as a guide, I’ve added the following:
lib/card_inventory_app/cards.ex
def get_card_by_set_and_number(set, collection_number) do
Repo.get_by(Card, set: set, collection_number: collection_number)
end
lib/card_inventory_app_web/router.ex
scope "/", CardInventoryAppWeb do
get "/cards/search", CardController, :search
end
CardController.ex
def search(conn, %{"set" => set, "collection_number" => collection_number} = _params) do
card = Cards.get_card_by_set_and_number(set, collection_number)
render(conn, :search, card: card)
end
controllers/card_html.ex
attr :card, :map, default: nil
def search_form(assigns)
controller/card_html/search.html.heex
<Layouts.app flash={@flash}>
<.header>
Search Cards
<:subtitle>Use this form to search for card records in your database.</:subtitle>
</.header>
<.search_form card={@card} action={~p"/cards/search"} return_to={~p"/cards/search"} />
</Layouts.app>
controller/card_html/search_form.html.heex
<.form :let={f} for={@card} action={@action}>
<.input field={f[:set]} type=“text” label=“Set” />
<.input field={f[:collection_number]} type=“number” label=“Collection number” />
<footer>
<.button variant="primary">Search for Card</.button>
<.button :if={@return_to} href={@return_to}>Cancel</.button>
</footer>
</.form>
The Cards.get_card_by_set_and_number function works in iex.
I’ve played around with the forms, changes etc but I still can’t get the search form to display.
The current error is as follows:
no function clause matching in CardInventoryAppWeb.CardController.search/2
The following arguments were given to CardInventoryAppWeb.CardController.search/2:
# 1
%Plug.Conn{adapter: {Bandit.Adapter, :...}, assigns: %{flash: %{}}, body_params: %{}, cookies: %{"_card_inventory_app_key" => "SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYWWcydmVZVUpIUFV4NXBrSjRzVUxjZG9S.vga988mp5AKDefNL_iBoB8y4E-SN7YUH4ssIDHSqiXI"}, halted: false, host: "localhost", method: "GET", owner: #PID<0.6597.0>, params: %{}, path_info: ["cards", "search"], path_params: %{}, port: 4000, private: %{:phoenix_live_reload => true, :phoenix_view => %{"html" => CardInventoryAppWeb.CardHTML, "json" => CardInventoryAppWeb.CardJSON}, :phoenix_endpoint => CardInventoryAppWeb.Endpoint, CardInventoryAppWeb.Router => [], :phoenix_action => :search, :phoenix_layout => %{}, :phoenix_controller => CardInventoryAppWeb.CardController, :phoenix_format => "html", :phoenix_root_layout => %{"html" => {CardInventoryAppWeb.Layouts, :root}}, :phoenix_router => CardInventoryAppWeb.Router, :plug_session_fetch => :done, :plug_session => %{"_csrf_token" => "Yg2veYUJHPUx5pkJ4sULcdoR"}, :before_send => [#Function<0.122245838/1 in Plug.CSRFProtection.call/2>, #Function<4.109347978/1 in Phoenix.Controller.fetch_flash/2>, #Function<0.76585640/1 in Plug.Session.before_send/2>, #Function<0.24950767/1 in Plug.Telemetry.call/2>, #Function<1.66527966/1 in Phoenix.LiveReloader.before_send_inject_reloader/3>], :phoenix_request_logger => {"request_logger", "request_logger"}}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{"_card_inventory_app_key" => "SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYWWcydmVZVUpIUFV4NXBrSjRzVUxjZG9S.vga988mp5AKDefNL_iBoB8y4E-SN7YUH4ssIDHSqiXI"}, req_headers: [{"host", "localhost:4000"}, {"accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}, {"sec-fetch-site", "none"}, {"upgrade-insecure-requests", "1"}, {"sec-fetch-mode", "navigate"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15"}, {"referer", "``http://localhost:4000/cards/search``"}, {"sec-fetch-dest", "document"}, {"cache-control", "max-age=0"}, {"accept-language", "en-GB,en;q=0.9"}, {"priority", "u=0, i"}, {"accept-encoding", "gzip, deflate"}, {"cookie", "_card_inventory_app_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYWWcydmVZVUpIUFV4NXBrSjRzVUxjZG9S.vga988mp5AKDefNL_iBoB8y4E-SN7YUH4ssIDHSqiXI"}, {"connection", "keep-alive"}], request_path: "/cards/search", resp_body: nil, ...}
I get the feeling I’m missing something basic, as it doesn’t feel like it should be this hard!
Any help would be greatly appreciated!






















