如何从网址中获取值并将其传递给操作?

时间:2016-12-08 22:30:14

标签: elixir phoenix-framework

我想填写html表单以创建评论正文,这样我就可以通过了解thread_iduser_id将其插入到数据库中。现在我无法在网址中获取thread_id

现在,我的路线看起来像这样:

        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内进行模式匹配"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 %>

1 个答案:

答案 0 :(得分:0)

我已设法使用Ecto.Changeset changes字段获取final InitialContext ic = new InitialContext(contextProperties); final QueueConnectionFactory factory = (QueueConnectionFactory) ic.lookup(factory); final Queue queue = (Queue) ic.lookup(queueName);

thread_id

但是,这是一种解决方法,它可以从操作<%= form_for @changeset, thread_comment_path(@conn,:create, @changeset.changes.thread_id), fn f -> %> 发送变更集,而不是真正从网址new/2中获取thread_id