#Need help understanding the Phoenix/Elixir way of solving this problem

10 messages · Page 1 of 1 (latest)

glass cave
#

Howdy folks 🤠

Basically, what I want to do is this:

# page controller - controllers/page_controller.ex
defmodule MyApp.PageController do
  # ...
  def create_game(conn, _params) do
    game_id = #generate_game_id
    # send user to /game/:game_id here, passing in the game id I just generated
  end
  def home(conn, _params) do
    render(conn, :home, layout: false, create_game)
  end
end

# home view - /controllers/page_html/home.html.heex
<button phx-click"@create_game()">Create Game</button>

On my home page, I want users to click a "Create Game" button that will start a Game process via a module I created called GameManager (Game is a Genserver module I created, and GameManager is also a GenServer that starts child processes under a Dynamic Supervisor). That should give me back a game_id, and then I want to send the user to a different route: /game/:game_id.

What is the right way to do this in Phoenix/Elixir? I tested the pieces that spawn the Game Processes and all of that seems to be working. Now I just want to wire this functionality up to a button and I'm struggling to see the right way to do it.

mild musk
#

phx-click is for Phoenix LiveView. Controllers are not LiveView. Which do you want? Do you want to use LiveView or traditional controllers?

glass cave
#

I feel like a traditional controller makes sense for the first route (the home page) because it's really just a couple of buttons, the second page will be a live view though

#

thanks for the speedy reply!

mild musk
glass cave
#

I feel like a traditional controller makes sense for the first route (the home page) because it's really just a couple of buttons, the second page will be a live view though

That being said, if it's much easier to wire up an interaction like this in a LiveView I'd rather do that instead. Especially if the alternative (normal controller) requires me to write more JS

mild musk
#

if you're new to phoenix, going one way or the other (not mixing them) is probably best imo

#

if it's a regular http request / controller then you just use href. If you're navigating between live views you want navigate