This is such a basic question that I'm failing to find the details online.
I have a model called 'Radios' and I wish to display the last radio added on to my home page - templates/page/index.html.eex
What I have so far:
radio.ex
defmodule Radios.Radio do
use Radios.Web, :model
import Ecto.Query
schema "radios" do
field :name, :string
field :desc, :string
field :price, :integer
field :text1, :string
field :text2, :string
field :text3, :string
field :text4, :string
field :mainimg, :string
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :desc, :price, :text1, :text2, :text3, :text4, :mainimg])
|> validate_required([:name, :desc, :price, :text1, :text2, :mainimg])
end
def sorted(query) do
from r in query,
order_by: [desc: r.inserted_at]
end
end
page_controller.ex
defmodule Radios.PageController do
use Radios.Web, :controller
alias Radios.Radio
def index(conn, _params) do
last_radio = Radio
|> Radio.sorted
|> Radios.Repo.one
render(conn, "index.html", radio: radio)
end
end
Page Template
<p class="title is-4"><%= @last_radio.name %></p>
I'm assuming I should be writing the query in page controller and not the radio controller?
All of this results in the following console error:
== Compilation error on file web/controllers/page_controller.ex ==
** (CompileError) web/controllers/page_controller.ex:11: undefined function radio/0
This is probably soooo basic, and imagine I'm missing something very simple, but what!?