我遇到了以下问题。我正在尝试将一些日期从变更集中绑定到隐藏字段。但我只得到隐藏的字段,没有任何值。
型号:
schema "events" do
field :name, :string
field :address, :string
field :location_x, :float
field :location_y, :float
field :date, Ecto.DateTime
field :description, :string
has_many :presences, Presence
has_many :users, through: [:presences, :user]
end
形式:
<%= form_for @changeset, @action, fn f -> %>
<!-- some unrealted code -->
<div class="form-group">
<%= label f, :description, "Description", class: "control-label" %>
<%= text_input f, :description, class: "form-control" %>
</div>
<%= hidden_input f, :location_y, id: "location_y", value: @changeset.model.location_y %>
<%= hidden_input f, :location_x, id: "location_x" %>
<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
正如您所看到的,我尝试过手动应用值:但也没有用。我看过和文档,我发现的只有“生成隐藏的输入”。所以假设它的行为与其他输入一样。
事件:
defmodule Kpsz.Model.Event do
use Kpsz.Web, :model
alias Kpsz.Model.Presence
schema "events" do
field :name, :string
field :address, :string
field :location_x, :float
field :location_y, :float
field :date, Ecto.DateTime
field :description, :string
has_many :presences, Presence
has_many :users, through: [:presences, :user]
end
@required_fields ~w(name address date description)
@optional_fields ~w(location_x location_y)
def changeset(user, params \\ :empty) do
user
|> cast(params, @required_fields, @optional_fields)
|> unique_constraint(:name)
end
end
控制器:
defmodule Kpsz.EventController do
use Kpsz.Web, :controller
import Kpsz.Authorize
plug :logged
plug :authorized_admin when action in [:new, :create, :edit, :update, :delete]
alias Kpsz.Model.Event
plug :scrub_params, "event" when action in [:create, :update]
def index(conn, _params) do
query = from w in Event,
select: w
events = Repo.all(query)
render(conn, "index.html", events: events)
end
def new(conn, _params) do
changeset = Event.changeset(%Event{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"event" => event_params}) do
changeset = Event.changeset(%Event{}, event_params)
case Repo.insert(changeset) do
{:ok, _event} ->
conn
|> put_flash(:info, "Event created successfully.")
|> redirect(to: event_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
event = Repo.get!(Event, id) |> Repo.preload([:users])
IO.inspect(event)
render(conn, "show.html", event: event)
end
def edit(conn, %{"id" => id}) do
event = Repo.get!(Event, id)
changeset = Event.changeset(event)
render(conn, "edit.html", event: event, changeset: changeset)
end
def update(conn, %{"id" => id, "event" => event_params}) do
event = Repo.get!(Event, id)
changeset = Event.changeset(event, event_params)
case Repo.update(changeset) do
{:ok, event} ->
conn
|> put_flash(:info, "Event updated successfully.")
|> redirect(to: event_path(conn, :show, event))
{:error, changeset} ->
render(conn, "edit.html", event: event, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
event = Repo.get!(Event, 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!(event)
conn
|> put_flash(:info, "Event deleted successfully.")
|> redirect(to: event_path(conn, :index))
end
end