【问题标题】:HTTP GET search form with PhoenixPhoenix 的 HTTP GET 搜索表单
【发布时间】:2017-11-13 15:50:03
【问题描述】:

我正在尝试制作一个简单的搜索表单来过滤我的资源列表。我已经制作了这样的表单,但它没有像我期望的 method: :get 那样将 q 作为查询参数放在 url 中

<%= form_for @conn, post_path(@conn, :index), [as: :search, method: :get], fn f -> %>
  <%= text_input f, :q, placeholder: "Search by title, content, etc." %>
<% end %>

当我以“term”作为搜索词提交此表单时,它会导致 search%5Bq%5D=term 被放置在 url 中(q 字段名称的 url 编码版本。)我预计只是 q=term .我的期望不正确吗?做一个简单的搜索表单的正确方法是什么?

【问题讨论】:

标签: elixir phoenix-framework


【解决方案1】:

表单中的所有字段都由参数as 包裹,例如qas: :search 变为 search[q]。您可以继续这样做并在控制器中提取此变量,如下所示:

def index(conn, %{"search" => %{"q" => q}}) do
  ...
end

或者,如果您希望该字段仅为q,您可以升级到phoenix_html 的v2.10.5 并删除as 参数。这个参数是可选的just 5 days ago

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多