i'm attempting display data databases in rethinkdb (using rethinkdb-elixir package hamiltop https://github.com/hamiltop/rethinkdb-elixir) in phoenix. i'm relatively new both, managed insert 2 tables , data tables. know because checked through rethinkdb's web gui.
now want display table data in html page of project. i've reduced errors one:
protocol phoenix.html.safe not implemented %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 fetching inserted carlos santos persons table (which must prevent not main issue) having error retrieving them phoenix project.
i've got index page in controller create tables , data. added new page: router.ex:
get "/users", userscontroller, :users /views/users_view.ex:
defmodule rethinkexample.usersview use rethinkexample.web, :view end users.html.eex:
<div class="jumbotron"> <p><%= @users %>!</p> </div> users_controller.ex
defmodule rethinkexample.userscontroller use rethinkexample.web, :controller use rethinkdb.query def users(conn, _params) q = table("users") |> filter(%{last_name: "santos"}) |> rethinkexample.database.run |> io.inspect render conn, "users.html", users: q end end
i deduce html code incorrect, because how display route specific id inside html tags. how can fetch data , display in html tag?
the problem here data structure in @users of type %rethinkdb.collection{} (source) cannot output using <%=...%>
you want iterate on users output them. like:
<%= user <- @users.data %> <p><%= "#{user["first_name"]} #{user["last_name"]}" %>!</p> <% end %> here using list comprehension iterate on items on @users.data array. common way output array of elements (such users, blog posts, comments, etc.) in eex.
you might want consider passing q.data though @users instead of q prevent having @users.data.
as aside, can use pattern matching inside list comprehension:
<%= %{"first_name" => first_name, "last_name" => last_name} <- @users.data %> <p><%= "#{first_name} #{last_name}" %>!</p> <% end %> this useful if don't plan on using many of fields in map.
Comments
Post a Comment