I’m trying to join my comments to a post, so that when a user visits /posts/:id/comments, he/she can get to view all the comments associated with the post.
This is my router
resources "/posts", PostController, except: [:new, :edit] do
resources "/comments", CommentController, except: [:new, :edit]
end
Then my Post Controller:
def index(conn, _params) do
post = Posts.list_posts()
render(conn, "index.json", posts: posts)
end
def create(conn, post_params) do
with {:ok, %Post{} = post} <-Posts.create_post(post_params) do #create_post is from post context
conn
|> put_status(:created)
|> put_resp_header("location", post_path(conn, :show, post))
|> render("show.json", post: post)
end
end
def show(conn, %{"id" => id}) do
post = Repo.get!(Post, id)
comment_changeset = Comment.changeset(%Comment{})
render(conn, "show.json", post: post, comment_changeset: comment_changeset)
end
My comment controller:
def index(conn, _params) do
comment= Comment.list_comment() #from comment context
render(conn, "index.json", comments: comments)
end
def create(conn, comment_params) do
post = Repo.get(Post, comment_params)
comment_changeset = Ecto.build_assoc(post, :comment, comment_params)
Repo.insert(comment_changeset)
conn
|> put_status(:created)
|> render("show.json")
end
def show(conn, %{"id" => id}) do
comment = Comment.get_comment!(id) #from comment context
render(conn, "show.json", comment: comment
end
When I try to add new comment in post/:id/comments, it throws this error:
(Ecto.Query.CastError) deps/ecto/lib/ecto/repo/queryable.ex:348: value `%{"description" => "bitcoin", "post_id" => "2", "name" => "ethereum"}` in `where` cannot be cast to type :id in query:
from m in Myapp.Posts.Post,
where: p.id == ^%{"description" => "bitcoin", "post_id" => "2", "name" => "ethereum"},
select: p
This is my Myapp.Posts.Post:
schema "posts" do
field :description, :string
field :name, :string
has_many :comments, Myapp.Comments.Comment
field :body, :string
timestamps()
end
@doc false
def changeset(post, attrs) do
post
|> cast(attrs, [:name, :description, :body])
|> validate_required([:name, :body ])
end
And my Myapp.Comments.Comment
schema "comments" do
field :description, :string
field :name, :string
belongs_to :market, Myapp.Posts.Post
timestamps()
end
@doc false
def changeset(comment, attrs) do
pair
|> cast(attrs, [:name, :description])
|> validate_required([:name, :description])
end
From the error, I think ecto is trying to pass the entire request parameters as p.id, but it shouldn’t be so, please how can I correct it? I have done many researches on it and tried some changes but got different errors.