Display Table Data From RethinkDB In Phoenix Framework
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 relati
Solution 1:
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 %>
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.
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.
Post a Comment for "Display Table Data From RethinkDB In Phoenix Framework"