微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在索引/显示模板上显示全名名字+姓氏

如何解决在索引/显示模板上显示全名名字+姓氏

我是Phoenix和Elixir的新手,但我努力学习。这可能是一个愚蠢的简单问题,但我不知道如何在该书的索引/显示页面显示该书作者的全名。

这是我在清单/book.ex中的查询

def get_author_full_name(author) do
  from b in "books",join: a in "authors",on: b.author_id == a.id,where: a.id == ^author,select: %{id: a.id,name: concat(a.first_name,' ',a.last_name)}
end

自定义sql

defmodule CustomsqlFunctions do
  defmacro concat(left,mid,right) do
    quote do
      fragment("concat(?,?,?)",unquote(left),unquote(mid),unquote(right))
    end
  end
end

在inventory.ex中

def display_author_full_name do
  Author
  |> Author.get_author_full_name()
  |> Repo.all()
end

在book_controller.ex

plug :load_author_full_name when action in [:index,:show]
defp load_author_full_name(conn,_) do
  assign(conn,:books,Bookstore.Inventory.display_author_full_name ())
end

我正在按照《编程Phoenix 1.4》一书中的说明显示类别的下拉列表,并做了一些小的更改以适合我的情况。有人可以指导我如何显示书作者的全名吗?我不知道我是否编写了与案例代码匹配的代码以及在视图和索引/显示页面中放入什么内容

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。