The "—live" option (phoenix 1.6.0.rc) does not seem to work properly

When generating a new app with live 1.6.0-rc.0 with “mix phx.new pento —live”

  1. no lib/pento_web/live was created.
  2. in section scope “/”, PentoWeb (router.ex), I see a
  • get “/”, PageController, :index
  • instead of live “/”, PageLive, :index
    Could anyone tell me what I missed here ?

Thanks

1 Like

Welcome @gareyte – you didn’t miss anything! Here’s what happened:

As of 1.6.0-rc.0 mix phx.new includes LiveView by default, but it no longer generates a live route for the homepage. This has been retroactively added to the changelog, sorry for the confusion!

Below I have included instructions for how to make one instead :slight_smile: We will give it a slightly different name to avoid any confusion when it comes to routes, so let’s call it MyPageLive.

First, create the following files (you can find the contents in this gist):

$ touch lib/pento_web/live/my_page_live.ex
$ touch lib/pento_web/live/my_page_live.html.heex

Next, add the following to your router scope, just under get “/”, PageController, :index:

live "/my", MyPageLive, :index

Then, start the server:

$ mix phx.server --open

Finally, navigate to http://localhost:4000/my and you should see page with a search bar in the hero section.

Let us know how it goes! :slight_smile:

17 Likes

Hello,

Thanks so much for your time and clear explanations !

Meanwhile, I had ended up with a setup similar to the one you propose.

As an elixir fan and phoenix newbie, your feedback makes me feel even better :wink:

The whole stack and community/contributers really impress me.

Kind regards

2 Likes

does this mean that running

mix phx.new myproject --live is same as mix phx.new myproject in phoenix 1.6?

1 Like

Yes. If you don’t want to include LiveView in a new 1.6 project you need to explicitly pass —no-live which will comment out the imports.

2 Likes
  1. Does the creation of /live directory necessary in Phoenix 1.6?

  2. WHen I write this in my router.ex
    live "/light", LightController, :light
    I get the following error

(ArgumentError) could not infer :as option because a live action was given and the LiveView does not have a "Live" suffix. Please pass :as explicitly or make sure your LiveView is named like "FooLive" or "FooLive.Index"```

Rename your ‘LightController’ to ‘LightLive’ so that it can infer it’s a LiveView controller.

The error tells you what is happening:

a live action was given and the LiveView does not have a “Live” suffix.

…and then it tells you how to fix:

Please pass :as explicitly or make sure your LiveView is named like “FooLive” or “FooLive.Index”

So make sure you are using the live macro to load a LiveView and not a Controller :slight_smile:

3 Likes

Much appreciated!

Thanks for elaborating on the error message.

1 Like

Is it necessary for me to create a lib/myapp_web/live directory in Phoenix 1.6? Or can I put LiveView files anywhere?

It is not necessary to create a directory specifically named live. However if you choose to use different/other names, you will likely want to update live_reload: [ patterns: [...] ] in your dev config so that changes in your paths will trigger a reload.