我正在尝试实施:create
行动。这是我的代码库。
defmodule Rumbl.Repo do
use Ecto.Repo, otp_app: :rumbl
end
表示User.ex
型号
defmodule Rumbl.User do
use Rumbl.Web, :model
schema "users" do
field :name, :string
field :username, :string
field :password, :string, virtual: true
field :password_hash, :string
timestamps()
end
def changeset(model, params \\ :empty) do
model
|> cast(params, ~w(name username), [])
|> validate_length(:username, min: 1, max: 20)
end
end
我的控制员:
defmodule Rumbl.UserController do
use Rumbl.Web, :controller
alias Rumbl.Repo
alias Rumbl.User
require Logger
def new(conn, _params) do
changeset = User.changeset(%User{})
render conn, "new.html", changeset: changeset
end
def create(conn, %{"user" => _params}) do
changeset = User.changeset(%User{}, _params)
case Repo.insert changeset do
{:ok, user} ->
conn
|> put_flash(:info, "#{user.name} created!")
|> redirect to: user_path(conn, :index)
{:error, changeset} ->
render conn, "new.html", changeset: changeset
end
end
def index(conn, _params) do
users = Repo.all(User)
render conn, "index.html", users: users
end
def show(conn, %{"id" => id}) do
user = Repo.get(User, String.to_integer(id))
render conn, "show.html", user: user
end
end
一旦我运行我的凤凰服务器它会打开新页面,当我发送请求时它会给我一个错误:
函数Rumbl.Repo.insert / 1未定义或私有
我错过了什么?
答案 0 :(得分:0)
我误解了Elixir的强权力量。实际上要解决我必须写的问题
sef create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert changeset do
{:ok, user} ->
conn
|> put_flash(:info, "#{user.name} created!")
|> redirect to: user_path(conn, :index)
{:error, changeset} ->
render conn, "new.html", changeset: changeset
end
end