【问题标题】:Form submission without parameter name in RailsRails中没有参数名称的表单提交
【发布时间】:2019-09-21 19:05:52
【问题描述】:

我有一个带有输入和提交按钮的表单。 如果用户输入“plane”并单击按钮,它将提交到 example.com/something?q=plane。我怎样才能让它去 example.com/something/plane

目前,我只用 HTML 编写它,但我认为我需要一个 rails 助手来做到这一点。 (它必须是一个“获取”请求)

<form action="/something" method="get">
  <input type="text" name="q" placeholder="Help me">
  <button type="submit">Results</button>
</form>

【问题讨论】:

  • 我认为您必须在单击提交后立即更新表单的url并通过js提交表单。或者你在 js 中做一个 API 调用。
  • 请求是GET还是POST有关系吗?在这种情况下,通常发布请求会达到您想要的效果
  • @ThorTL67 它必须是一个获取请求..

标签: html ruby-on-rails forms


【解决方案1】:

在表单中使用 post 方法。

<form method="post"></form>

【讨论】:

    【解决方案2】:
      $(document).on("submit", "form[data-turboform]", function(e) {
        var query = $(this).serialize().substring($(this).serialize().indexOf('q=') + 2);
        Turbolinks.visit(this.action + '/' + query);
        return false;
      });
    

    此 jQuery 代码将在表单提交时触发并生成链接,然后使用 Turbolinks.visit 转到路由。请记住,这仅适用于 GET 请求

    <%= form_tag orders_path, method: :get, local: true, enforce_utf8: false, 'data-turboform': true do %>
      <%= text_field_tag :q, nil, class: 'form-control', placeholder: 'Help Me'  %>
      <%= submit_tag 'Submit'%>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      相关资源
      最近更新 更多