我尝试使用和配置arc和arc_ecto,如其自述文件中所述。不幸的是我收到了错误。我该如何解决?
mix phoenix.new my_app --database mysql
cd my_app
mix ecto.create
mix deps.get
mix phoenix.gen.html User users name
mix arc.g avatar
mix ecto.gen.migration add_avatar_to_users
该迁移的代码:
defmodule MyApp.Repo.Migrations.AddAvatarToUsers do
use Ecto.Migration
def change do
alter table :users do
add :avatar, :string
end
end
end
比我运行迁移:
mix ecto.migrate
我启动服务器导致错误:
$ mix ecto.server
== Compilation error on file web/controllers/user_controller.ex ==
** (CompileError) web/controllers/user_controller.ex:14: MyApp.User.__struct__/0 is undefined, cannot expand struct MyApp.User
(elixir) src/elixir_map.erl:58: :elixir_map.translate_struct/4
(stdlib) lists.erl:1353: :lists.mapfoldl/3
web/controllers/user_controller.ex:13: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
== Compilation error on file web/models/user.ex ==
** (CompileError) web/models/user.ex:3: module Arc.Ecto.Schema is not loaded and could not be found
(elixir) expanding macro: Kernel.use/1
web/models/user.ex:3: MyApp.User (module)
(elixir) lib/kernel/parallel_compiler.ex:100: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8
$
我使用 web / models / user.ex 的确切README副本:
defmodule MyApp.User do
use MyApp.Web, :model
use Arc.Ecto.Schema
schema "users" do
field :name, :string
field :avatar, MyApp.Avatar.Type
end
@required_fields ~w()
@optional_fields ~w(name)
@required_file_fields ~w()
@optional_file_fields ~w(avatar)
@doc """
Creates a changeset based on the `data` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(user, params \\ :invalid) do
user
|> cast(params, @required_fields, @optional_fields)
|> cast_attachments(params, @required_file_fields, @optional_file_fields)
end
end
网络/上传/ avatar.ex
defmodule MyApp.Avatar do
use Arc.Definition
use Arc.Ecto.Definition
@versions [:original]
end
mix.exs
defmodule MyApp.Mixfile do
use Mix.Project
[...]
# Specifies your project dependencies.
#
# Type `mix help deps` for examples and options.
defp deps do
[{:phoenix, "~> 1.1.0"},
{:phoenix_ecto, "~> 2.0"},
{:mariaex, ">= 0.0.0"},
{:phoenix_html, "~> 2.3"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.9"},
{:cowboy, "~> 1.0"},
{:arc, "~> 0.5.2"},
{:arc_ecto, "~> 0.3.2"}]
end
# Aliases are shortcut or tasks specific to the current project.
# For example, to create, migrate and run the seeds file at once:
#
# $ mix ecto.setup
#
# See the documentation for `Mix` for more info on aliases.
defp aliases do
["ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"]]
end
end
用户控制器 web / controllers / user_controller.ex 未更改:
defmodule MyApp.UserController do
use MyApp.Web, :controller
alias MyApp.User
plug :scrub_params, "user" when action in [:create, :update]
def index(conn, _params) do
users = Repo.all(User)
render(conn, "index.html", users: users)
end
def new(conn, _params) do
changeset = User.changeset(%User{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, _user} ->
conn
|> put_flash(:info, "User created successfully.")
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
user = Repo.get!(User, id)
render(conn, "show.html", user: user)
end
def edit(conn, %{"id" => id}) do
user = Repo.get!(User, id)
changeset = User.changeset(user)
render(conn, "edit.html", user: user, changeset: changeset)
end
def update(conn, %{"id" => id, "user" => user_params}) do
user = Repo.get!(User, id)
changeset = User.changeset(user, user_params)
case Repo.update(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "User updated successfully.")
|> redirect(to: user_path(conn, :show, user))
{:error, changeset} ->
render(conn, "edit.html", user: user, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
user = Repo.get!(User, id)
# Here we use delete! (with a bang) because we expect
# it to always work (and if it does not, it will raise).
Repo.delete!(user)
conn
|> put_flash(:info, "User deleted successfully.")
|> redirect(to: user_path(conn, :index))
end
end
答案 0 :(得分:1)
此修复是在 web / models / user.ex 中使用use Acr.Ecto.Model
代替Arc.Ecto.Schema
:
defmodule MyApp.User do
use MyApp.Web, :model
use Arc.Ecto.Model
[...]
end