Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a user #7

Closed
idkjs opened this issue Nov 3, 2020 · 7 comments
Closed

Creating a user #7

idkjs opened this issue Nov 3, 2020 · 7 comments

Comments

@idkjs
Copy link

idkjs commented Nov 3, 2020

Hi folks/ @SophieDeBenedetto. Thank you for sharing this.

I am an elixir beginner. Is there an instruction somewhere on how to create a user in the console? Can't seem to find it.

Thank you.

@SophieDeBenedetto
Copy link
Collaborator

Hi @idkjs! You should be able to create a user through the browser like this:

  • start the server with mix phx.server
  • Visit the /users/new route
  • Fill out the registration form and sign up!

@idkjs
Copy link
Author

idkjs commented Nov 3, 2020

Tried that. I get a browser error asking me to run migrations which doesn't work. I am afk and will try to get you a better error ASAP. Thank you.

@idkjs
Copy link
Author

idkjs commented Nov 4, 2020

@SophieDeBenedetto thank you. That helped and I see it was mentioned in #1 .

The readme says that the seed should have created some chats and users though it seems like it doesnt do anything.

This is the seed file:

# Script for populating the database. You can run it as:
#
#     mix run priv/repo/seeds.exs
#
# Inside the script, you can read and write to any of your
# repositories directly:
#
#     Chat.Repo.insert!(%Chat.SomeSchema{})
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.

Here is the chats link in the browser.

Screen Shot 2020-11-04 at 12 40 09 PM

I feel like i'm missing something since the code in tutorial branch doesnt seem to match what is in the tutorial. Am I missing something?

Thanks again for taking the time to teach.

Here is the terminal history if it helps: https://gist.github.com/idkjs/7ad9ecf509e3fa1933976a7e6270cea9

@SophieDeBenedetto
Copy link
Collaborator

Hi again! You need to checkout the tutorial branch and work off of that. You can see that the seed file in the tutorial branch is populated https://github.com/elixirschool/live-view-chat/blob/tutorial/priv/repo/seeds.exs

@idkjs
Copy link
Author

idkjs commented Nov 4, 2020

Thanks. I kinda gave up on the tutorial branch because I didnt understand what we were supposed to be following along with. Is the tutorial branch a start position?

For example, the first step in the blog is

# lib/phat_web/controllers/chat_controller.ex

defmodule PhatWeb.ChatController do
  use PhatWeb, :controller
  alias Phat.Chats
  alias Phoenix.LiveView
  alias PhatWeb.ChatLiveView

  def show(conn, %{"id" => chat_id}) do
    chat = Chats.get_chat(chat_id)

    LiveView.Controller.live_render(
      conn,
      ChatLiveView,
      session: %{chat: chat, current_user: conn.assigns.current_user}
    )
  end
end

Then I see in the tutorial branch, that file has the following content:

defmodule PhatWeb.ChatController do
use PhatWeb, :controller
alias Phat.Chats
alias Phoenix.LiveView
alias PhatWeb.ChatLiveView
plug :authenticate_user
def index(conn, _params) do
chats = Chats.list_chats()
render(conn, "index.html", chats: chats)
end
def show(conn, %{"id" => chat_id}) do
chat = Chats.get_chat(chat_id)
LiveView.Controller.live_render(
conn,
ChatLiveView,
session: %{chat: chat, current_user: conn.assigns.current_user}
)
end
defp authenticate_user(conn, _) do
case get_session(conn, :user_id) do
nil ->
conn
|> Phoenix.Controller.put_flash(:error, "Login required")
|> Phoenix.Controller.redirect(to: "/sessions/new")
|> halt()
user_id ->
assign(conn, :current_user, Phat.Accounts.get_user(user_id))
end
end
end

So it looks its already been done or this has more code in it then your blog? Idk. What are we supposed to do here if we are following along.

Probably me. Any guidance would be appreciated. Thank you.

@SophieDeBenedetto
Copy link
Collaborator

If I recall correctly the tutorial branch has the starting state of the code for the blog post walk-through. Per this line from the blog post:

The starting state of the tutorial branch includes the chat domain model, routes, controller and the initial state of the LiveView, described below

You will find some completed code in the tutorial branch that takes care of the initial state of the app for the blog post. Then, the blog post walks you through adding additional features.

@idkjs
Copy link
Author

idkjs commented Nov 10, 2020

Ok. Thank you. Given my inexperience with elixir, I did not know what to make of that line. I saw it but was guessing as to what exactly it affected. I will look at it again and try to think through more carefully. Thank you for taking the time, @SophieDeBenedetto .

@idkjs idkjs closed this as completed Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants