私はコメントボディを作成するためにhtmlフォームを埋めたいので、thread_id
とuser_id
を知ってdbに挿入することができます。今、私はURLにthread_id
を入れても困っています。URLから値を取得してアクションに渡す方法は?
さて、私はこのように見えるのルート抱えている:私はthread_id = 7
私」を取得する必要があります
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
とURL http://localhost:4000/manage/thread/7/comments/new
からこの
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
ようになりコントローラでアクションをそれを得るにはthread_id = 1
アクション内に作成/ 2パターンマッチ"thread_id"=> thread_id
。内部アクションnew/2私はthread_id = 7
のhersパターンマッチから、私は接続経由で送信すると思ったが、それはうまくいきませんでした。 new.htmlからの新しいconnが返されたので、私はそれを推測していますか?ここで
も、私はEcto.Changeset changesフィールドを使用してthread_id
を得ることができたこと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、@current_user)の代わりに' thread_comment_path(@conn、:create、@id_thread) 'を試してください。 – Dogbert
次に、GET/manage/thread/7/comments/newでエラーArgumentErrorを取得します。 @ id_threadはeexテンプレートでは使用できません。 適切な割り当てがすべて設定されていることを確認してください。この が子テンプレートの場合、自動的に転送されないので、割り当てが親テンプレート によって明示的に指定されていることを確認してください。 利用可能な割り当て:[:changeset、:conn、:current_user、:view_module、:view_template] – Daniel
おっと、 'thread_comment_path(@conn、:create、@ changeset.data.thread_id)'を試してください。 – Dogbert