使用Elixir和Phoenix在索引页面上显示记录

时间:2017-11-22 20:50:30

标签: elixir phoenix-framework

我正在尝试使用理解来显示数据库中的记录。但我不能Comprehend(恶作剧)出了什么问题?错误信息,虽然我确定它告诉我什么是错的,我不明白它想说的是什么。

ERROR:

UndefinedFunctionError at GET /leagues
function Statcasters.Schema.League.fetch/2 is undefined (Statcasters.Schema.League does not implement the Access behaviour)


%Statcasters.Schema.League{__meta__: #Ecto.Schema.Metadata<:loaded, "leagues">, id: 1, inserted_at: ~N[2017-11-22 03:36:47.999950], name: "nae", teams: #Ecto.Association.NotLoaded<association :teams is not loaded>, updated_at: ~N[2017-11-22 03:36:47.999956], user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1}

VIEW:

<h1>Join a League</h1>

<%= for league <- @leagues do %>
  <%= league["name"] %>
<% end %>

模型:

联赛:

defmodule Statcasters.Schema.League do
  use Ecto.Schema
  import Ecto
  import Ecto.Changeset
  import Ecto.Query

  schema "leagues" do
    field :name, :string
    has_many :teams, Statcasters.Teams.Team
    belongs_to :user, Statcasters.Coherence.User

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
      |> cast(params, [:name, :user_id])
      |> validate_required([:name, :user_id])
  end
end

USER:

defmodule Statcasters.Coherence.User do
  @moduledoc false
  use Ecto.Schema
  use Coherence.Schema

  schema "users" do
    field :name, :string
    field :email, :string
    coherence_schema()
    has_many :leagues, Statcasters.Schema.League

    timestamps()
  end

  def changeset(model, params \\ %{}) do
    model
    |> cast(params, [:name, :email] ++ coherence_fields())
    |> validate_required([:name, :email])
    |> validate_format(:email, ~r/@/)
    |> unique_constraint(:email)
    |> validate_coherence(params)
  end

  def changeset(model, params, :password) do
    model
    |> cast(params, ~w(password password_confirmation reset_password_token reset_password_sent_at))
    |> validate_coherence_password_reset(params)
  end
end

LEAGUE CONTROLLER:

  def index(conn, _params) do
    leagues = Repo.all(League)

    render(conn, "index.html", leagues: leagues)
  end

同样,我要做的就是在联盟索引页面上按名称列出系统中的所有联赛。谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

你应该替换这一行:

<%= league["name"] %>

用这个:

<%= league.name %>

league是一个结构,为了能够读取它的属性,你需要使用点符号。