elixir - Follow back functionality -
i have twitter application users
can follow each other through connection
model. in table lists people follow @user
i'd implement link follow back
. can link or have forms , display buttons? how setup changesets these forms?
web/models/user.ex
defmodule myapp.user use myapp.web, :model use arc.ecto.model schema "users" field :last_name, :string has_many :follower_connections, myapp.connection, foreign_key: :followee_id has_many :followers, through: [:follower_connections, :follower] [...]
web/models/connection.ex
defmodule myapp.connection use myapp.web, :model schema "connections" belongs_to :follower, myapp.user belongs_to :followee, myapp.user [...]
web/controllers/user_controller.ex
[...] def show(conn, %{"id" => id}) user = repo.get!(user, id) |> repo.preload([:followers, :follower_connections]) conn |> assign(:user, user) |> render("show.html") end [...]
web/templates/user/show.html.eex
[...] <table> <tbody> <%= connection <- @user.follower_connections %> <tr> <td><%= link connection.follower.last_name %></td> <td> <%= link ???????? "follow back" %> </td> </tr> <% end %> </tbody> </table> [...]
the link ????????
part problem.
you can link action implementing follow this:
link "follow back", to: user_path(@conn, :follow_back, user_id, [])
the header this:
def follow_back(conn, %{"id" => id})
in router:
get "/follow_back/:id", usercontroller, :follow_back
this solution skips changesets @ all. if want custom validation on data there great post on using embedded_schema
forms aren't directly persisted database: http://blog.plataformatec.com.br/2016/05/ectos-insert_all-and-schemaless-queries/
the docs on html link here: https://hexdocs.pm/phoenix_html/phoenix.html.link.html tutorial on routing: http://www.phoenixframework.org/docs/routing , tutorial on controllers: http://www.phoenixframework.org/docs/controllers
Comments
Post a Comment