【发布时间】:2016-12-08 22:30:14
【问题描述】:
我想填写用于创建评论正文的 html 表单,这样我就可以通过知道 thread_id 和 user_id 将其插入到 db 中。现在我无法获取thread_id即使它在 url 中。
现在,我的路线如下所示:
thread_path GET /manage/thread Rumbl.ThreadController :index
thread_path GET /manage/thread/:id/edit Rumbl.ThreadController :edit
thread_path GET /manage/thread/new Rumbl.ThreadController :new
thread_path GET /manage/thread/:id Rumbl.ThreadController :show
thread_path POST /manage/thread Rumbl.ThreadController :create
thread_path PATCH /manage/thread/:id Rumbl.ThreadController :update
PUT /manage/thread/:id Rumbl.ThreadController :update
thread_path DELETE /manage/thread/:id Rumbl.ThreadController :delete
thread_comment_path GET /manage/thread/:thread_id/comments Rumbl.CommentController :index
thread_comment_path GET /manage/thread/:thread_id/comments/:id/edit Rumbl.CommentController :edit
thread_comment_path GET /manage/thread/:thread_id/comments/new Rumbl.CommentController :new
thread_comment_path GET /manage/thread/:thread_id/comments/:id Rumbl.CommentController :show
thread_comment_path POST /manage/thread/:thread_id/comments Rumbl.CommentController :create
thread_comment_path DELETE /manage/thread/:thread_id/comments/:id Rumbl.CommentController :delete
控制器中的动作看起来像这样
def new(conn, %{"thread_id" => thread_id}) do
changeset = Comment.changeset(
%Comment{},
%{user_id: conn.assigns.current_user.id,
thread_id: thread_id
})
render(conn,"new.html", changeset: changeset)
end
def create(conn, %{"comment" => %{"content" => content}, "thread_id"=> thread_id}) do
user_id = conn.assigns.current_user.id
changeset = Comment.changeset(
%Comment{
content: content,
user_id: user_id,
thread_id: to_integer(thread_id)
})
case Repo.insert(changeset) do
{:ok, _comment} ->
conn
|> put_flash(:info, "Comment created successfully.")
|> redirect(to: thread_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
来自网址http://localhost:4000/manage/thread/7/comments/new
我应该得到thread_id = 7,但我得到的是thread_id = 1内部操作create/2,用于模式匹配"thread_id"=> thread_id。内部动作 new/2 我有来自她的模式匹配的thread_id = 7,所以我想通过连接发送它,但那不起作用。我猜是因为 new.html 中的新 conn 被返回了?
这里也是那个new.html文件
<h2>New comment</h2>
<%= form_for @changeset, thread_comment_path(@conn,:create, @current_user), fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<div class="form-group">
<%= label f, :content, class: "control-label" %>
<%= textarea f, :content, class: "form-control" %>
<%= error_tag f, :content %>
</div>
<%= submit "Create Comment", class: "btn btn-primary" %>
<% end %>
【问题讨论】:
-
尝试
thread_comment_path(@conn, :create, @id_thread)而不是thread_comment_path(@conn,:create, @current_user)。 -
然后我在 GET /manage/thread/7/cmets/new assign @id_thread 在 eex 模板中不可用时收到错误 ArgumentError。请确保已设置所有正确的分配。如果这是子模板,请确保父模板明确给出分配,因为它们不会自动转发。可用分配:[:changeset, :conn, :current_user, :view_module, :view_template]
-
糟糕,试试
thread_comment_path(@conn, :create, @changeset.data.thread_id)。 -
ArgumentError at GET /manage/thread/7/comments/new cannot convert nil to param即使我在Params在错误网页中看到thread_id是“7”:/