2
이것은 온라인에서 세부 정보를 찾지 못하는 기본적인 질문입니다.Phoenix/Ecto를 사용하여 데이터베이스에 마지막 레코드 표시
나는 '라디오'라는 모델을하고 난 마지막 라디오가 내 홈페이지에 추가 표시 할 - templates/page/index.html.eex
내가 지금까지 가지고 :
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
내가 페이지 컨트롤러에서 쿼리가 아닌 무선 컨트롤러를 작성해야 겠지
<p class="title is-4"><%= @last_radio.name %></p>
페이지 템플릿?
이 모든 것은 다음과 같은 콘솔 오류가 발생합니다 :
== Compilation error on file web/controllers/page_controller.ex ==
** (CompileError) web/controllers/page_controller.ex:11: undefined function radio/0
이 아마 soooo는 기본이고, 나는 아주 단순하지만 뭔가를 누락 상상!?
정확합니다. 그게 효과가있다! 그리고 추가 쿼리를 생성하면 렌더링 배열에 계속 추가 할 수 있습니까? –
예. 'render (conn, "index.html", last_radio : last_radio, foo : foo, bar : bar)'를 호출합니다. 원하는만큼의 값을 지정할 수 있습니다. –
위대한, 감사합니다 @ justinwood. –