在Phoenix Framework中显示来自RethinkDB的表数据 [英] Display table data from RethinkDB in Phoenix Framework
问题描述
我正在尝试显示RethinkDB中数据库中的数据(使用Hamiltop中的rethinkdb-elixir包 https凤凰城的://://github.com/hamiltop/rethinkdb-elixir ).我对这两个都比较陌生,但是我已经设法在两个表中插入了两个表和一些数据.我知道这一点是因为我通过RethinkDB的Web GUI对其进行了检查.
I'm attempting to display data from my databases in RethinkDB (using the rethinkdb-elixir package from Hamiltop https://github.com/hamiltop/rethinkdb-elixir) in Phoenix. I'm relatively new to both, but I already managed to insert two tables and some data into those tables. I know this because I checked it through RethinkDB's web GUI.
现在,我想在项目的html页面中显示表数据. 我已将错误减少为一个:
Now I want to display table data in an html page of my project. I've reduced the errors to one:
protocol Phoenix.HTML.Safe not implemented for %RethinkDB.Collection{data: [%{"first_name" => "Carlos", "id" => "4be8adc3-0973-45dc-bdb8-7a4dac6528d5", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "c84658fc-e4a4-4cb6-8107-b011ca996abd", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "c09fe081-379a-4334-97a3-31c5503c8c61", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "cf0c0ad3-3152-40f0-b613-5b051a314b51", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "ca28a714-ed54-4ebd-8707-d53170ead0f7", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "1ea77c0f-538c-4663-be92-499f16996594", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "1ea74846-0860-4ae5-95f5-674860cf7fc6", "last_name" => "Santos"}]}
很明显,它正在从表中提取所有插入的Carlos Santos人员(我也必须防止,但这不是我的主要问题),但是在将其检索到Phoenix项目时出错.
Clearly it is fetching all the inserted Carlos Santos persons from the table (which I also must prevent but that is not my main issue) but having an error retrieving them to my Phoenix project.
我有一个索引页,在其中创建表和数据的控制器. 然后我添加了一个新页面: router.ex :
I've got an index page in whose controller I create the tables and data. Then I added a new page: router.ex:
get "/users", UsersController, :users
/views/users_view.ex :
defmodule RethinkExample.UsersView do
use RethinkExample.Web, :view
end
users.html.eex :
<div class="jumbotron">
<p><%= @users %>!</p>
</div>
users_controller.ex
defmodule RethinkExample.UsersController do
use RethinkExample.Web, :controller
use RethinkDB.Query
def users(conn, _params) do
q = table("users")
|> filter(%{last_name: "Santos"})
|> RethinkExample.Database.run
|> IO.inspect
render conn, "users.html", users: q
end
结束
我推断html代码也不正确,因为这是我将在html标签内显示特定于路由的ID的方式. 如何成功获取数据,然后将其显示在html标签中?
I deduce that the html code is also incorrect, because this is how I would display the route specific id inside the html tags. How can I fetch the data successfully and then display it in a html tag?
推荐答案
此处的问题是@users
中的数据结构为%RethinkDB.Collection{}
类型(
The problem here is that your data structure in @users
is of type %RethinkDB.Collection{}
(source) which cannot be output using <%=...%>
您可能希望遍历用户以输出它们.像这样:
You will likely want to iterate over your users to output them. Something like:
<%= for user <- @users.data do %>
<p><%= "#{user["first_name"]} #{user["last_name"]}" %>!</p>
<% end %>
在这里,我们使用列表理解来遍历@users.data
数组.这是在EEx中输出一系列元素(例如用户,博客文章,评论等)的常用方法.
Here we are using a list comprehension to iterate over all the items on the @users.data
array. This is a common way to output an array of elements (such as users, blog posts, comments, etc.) in EEx.
您可能还想考虑将q.data
传递为@users
而不是q
,以防止必须执行@users.data
.
You might also want to consider passing q.data
though as @users
instead of q
to prevent having to do @users.data
.
顺便说一句,您还可以在列表推导中使用模式匹配:
As an aside, you can also use pattern matching inside the list comprehension:
<%= for %{"first_name" => first_name, "last_name" => last_name} <- @users.data do %>
<p><%= "#{first_name} #{last_name}" %>!</p>
<% end %>
如果您不打算使用地图中的许多字段,这将很有用.
This is useful if you don't plan on using many of the fields in the map.
这篇关于在Phoenix Framework中显示来自RethinkDB的表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!