Displaying table data from RethinkDB in Phoenix Framework
I am trying to display data from my databases in RethinkDB (using the rethinkdb-elixir package from Hamiltop https://github.com/hamiltop/rethinkdb-elixir ) in Phoenix. I am relatively new to both, but I have already managed to insert two tables and some data into these tables. I know this because I tested it through the RethinkDB web interface.
Now I want to display the table data in the 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"}]}
Clearly it is pulling all the inserted faces of Carlos Santos from the table (which I should also prevent, but this is not my main problem), but with an error that returns them to my Phoenix project.
I have an index page in which controller I am creating 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
end
I deduce that the html code is also wrong because this is how I will display the route id in the html tags. How can I retrieve the data successfully and then display it in the html tag?
source to share
The problem is that your data structure in @users
has a type %RethinkDB.Collection{}
( source ) that cannot be inferred using<%=...%>
You will most likely want to iterate over your users for their output. Something like:
<%= for user <- @users.data do %>
<p><%= "#{user["first_name"]} #{user["last_name"]}" %>!</p>
<% end %>
Here we use the description of the list to iterate over all the elements of the array @users.data
. This is a common way to output an array of items (such as users, blog posts, comments, etc.) in EEx.
You may also want to consider transferring q.data
, but like @users
instead q
, to prevent execution @users.data
.
As an aside, you can also use pattern matching inside a 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 do not plan to use many of the fields on the map.
source to share