【发布时间】:2018-10-11 23:24:14
【问题描述】:
我正在尝试将我的 cmets 加入一个帖子,以便当用户访问 /posts/:id/cmets 时,他/她可以查看与该帖子关联的所有 cmets。
这是我的路由器
resources "/posts", PostController, except: [:new, :edit] do
resources "/comments", CommentController, except: [:new, :edit]
end
然后是我的帖子控制器:
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
我的评论控制器:
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
当我尝试在 post/:id/cmets 中添加新评论时,它会抛出此错误:
(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
这是我的 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
还有我的 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
从错误中,我认为ecto试图将整个请求参数作为p.id传递,但它不应该是这样,请问我该如何纠正它?我对此进行了很多研究并尝试了一些更改,但得到了不同的错误。
【问题讨论】:
-
这是我最初的评论控制器:
def create(conn, %{"post_id" => id, "comment_params" => comment_params}) do post = Repo.get(Post, id) comment_changeset = Ecto.build_assoc(post, :comment, comment_params) Repo.insert(comment_changeset)但这给了我这个错误:[debug] ** (Phoenix.ActionClauseError) could not find a matching MyappWeb.CommentController.create clause. This typically happens when there is a parameter mismatch but may also happen when any of the other action arguments do not match.
标签: elixir phoenix-framework ecto