我在Phoenix控制器中有一个查询,该控制器获取学生列表,并且对于列表中的每一行,在其id上与血型组连接并获取血型组名称。我想在我生成的eex模板中显示这个,但是我遇到了错误。
这是学生模式:
schema "bloodgroups" do
field :name, :string
timestamps()
end
以下是BloodGroup模型:
def query do
query = from s in Student,
join: b in BloodGroup, on: s.bloodgroup_id == b.id,
select: [s.firstname, s.lastname, s.birthday, s.joined_on, s.bloodgroup_id, b.name]
end
def index(conn, _params) do
students = Repo.all(query)
render(conn, "index.html", student_info: students)
end
我在控制器中获取数据如下:
<%= for student <- @student_info do %>
<tr>
<td><%= Enum.fetch(student, 0) %></td>
<td><%= student[:lastname] %></td>
<td><%= student[:birthday] %></td>
<td><%= student[:joined_on] %></td>
<td><%= student[:name] %></td>
<td class="text-right">
<%= link "Show", to: student_path(@conn, :show, student), class: "btn btn-default btn-xs" %>
<%= link "Edit", to: student_path(@conn, :edit, student), class: "btn btn-default btn-xs" %>
<%= link "Delete", to: student_path(@conn, :delete, student), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %>
</td>
</tr>
<% end %>
并在模板中显示如下:
Enum.fetch
但是,使用student[:firstname]
和Argument error
表单并不能正常工作并抛出protocol Phoenix.HTML.Safe not implemented
或 private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
var source = sender as CheckBox;
if (source.Checked == true)
{
this.numericUpDown1.Text = "TextWhenChecked";
this.labelAtTheNumericUpDown.Text = "TextWhenChecked";
}
else
{
this.numericUpDown1.Text = "TextWhenUnchecked";
this.label1.Text = "TextWhenUnchecked";
}
}
我想显示模板中控制器发送的内容,但是我遇到了这些错误。有一个更好的方法吗?我是凤凰城和Elixir的新手。
答案 0 :(得分:3)
根据您发布的查询,您应该返回包含Repo.all
的列表列表。您可以使用模式匹配来提取数据,例如:
<%= for [firstname, lastname, birthday, joined_on, bloodgroup_id, name] <- @student_info do %>
但是,这不是惯用的。你真的应该在这里使用Ecto的关系。
将BloodGroup
belongs_to
作为Student
与schema "students" do
...
belongs_to :bloodgroup, BloodGroup, foreign_key: :bloodgroup_id
end
的关系添加:
students = Repo.all(Student) |> Repo.preload(:bloodgroup)
然后执行以下查询:
<%= for student <- @student_info do %>
<tr>
<td><%= student.lastname %></td>
<td><%= student.birthday %></td>
<td><%= student.joined_on %></td>
<td><%= student.bloodgroup.name %></td>
<td class="text-right">
<%= link "Show", to: student_path(@conn, :show, student), class: "btn btn-default btn-xs" %>
<%= link "Edit", to: student_path(@conn, :edit, student), class: "btn btn-default btn-xs" %>
<%= link "Delete", to: student_path(@conn, :delete, student), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-danger btn-xs" %>
</td>
</tr>
<% end %>
然后你的模板就变成了:
var main = $(".main-value");
mainValue = parseInt(main.html());
$(".new-value").html(mainValue);
所有代码均未经测试。如果这不起作用,请告诉我。这可能是某个地方的错字。