【问题标题】:Ruby on Rails select2 submit doesn't workRuby on Rails select2 提交不起作用
【发布时间】:2018-05-09 10:42:12
【问题描述】:
  https://select2.org/getting-started/basic-usage

我想像上面链接中的第一个示例一样搜索帖子标题。

代码:

<!-- search -->
      <div class="card my-auto">
        <%= form_with url: posts_path, method: :get, local: :true do |f| %>
          <div class="card-body">
            <p>Search for a Post title.</p>
            <%= f.collection_select(:post_id, Post.all, :id, :title, {include_blank: 'Post titles'}, {class:'selectbooktitle form-control'}) %>
            <hr>
            <div class="input-group">
                <span class="input-group-btn">
                  <%= f.submit 'Search', class: 'btn btn-outline-success'%>

                </span>
        <% end %>
        </div>
        </div>
      </div>

这是单击提交时来自我的服务器的请求。

Started GET "/posts?utf8=%E2%9C%93&post_id=16&commit=Search" for 127.0.0.1 at 2018-05-09 14:18:51 +0200
Processing by PostsController#index as HTML
  Parameters: {"utf8"=>"✓", "post_id"=>"16", "commit"=>"Search"}
  Rendering posts/index.html.erb within layouts/application
  Post Load (0.3ms)  SELECT "posts".* FROM "posts"
  Post Load (0.3ms)  SELECT "posts".* FROM "posts" ORDER BY created_at DESC
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 4], ["LIMIT", 1]]
  Rendered posts/index.html.erb within layouts/application (10.2ms)
  Category Load (0.3ms)  SELECT "categories".* FROM "categories"
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  Rendered layouts/_navbar.html.erb (4.5ms)
  Rendered layouts/_alerts.html.erb (0.4ms)
  Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 59ms (Views: 55.0ms | ActiveRecord: 1.9ms)

指向此 URL:

http://localhost:3000/posts?utf8=%E2%9C%93&post_id=20&commit=Search

它应该指向以下 URL:

http://localhost:3000/posts/20

我做错了什么?

提前感谢您的帮助!

【问题讨论】:

  • 好的。你试过什么?你在哪里卡住了?
  • 我尝试实现我发布的 f.collection_select 行 - 说 NoMethod 错误 collection_select
  • 那是因为fnil#form_tag 不会将任何参数传递给它的块。您必须改用 #form_with (如果您想保持相同的行为,请设置 local: true)。 api.rubyonrails.org/classes/ActionView/Helpers/…
  • @JohanWentholt 是的!我的表单现在可以工作了,但是我仍然遇到的唯一问题是,每当我尝试提交表单时,它实际上不会将页面切换到选择的搜索对象。它会将 URL 更改为 localhost:3000/… 但不会实际更改页面。
  • 如果您想要与示例中完全相同的表单,您必须设置以下内容:form_with url: posts_path, method: :get, local: :true。你设置了这些选项吗?如果是,您在服务器上收到什么请求?

标签: ruby-on-rails ruby jquery-select2 select2-rails


【解决方案1】:

Rails 框架将您发送到该 URL 并没有错,因为您将 form_with :url 选项设置为 posts_path。这等于/posts

您要做的是根据选择的内容更改表单操作。为此,您需要 JavaScript 来动态更改表单操作。

这是一个简化的例子:

app/views/some_directory/some_file.html.erb

<%= form_with url: posts_path, method: :get, local: true do |form| %>
  <% options = {include_blank: 'Post titles'} %>
  <% html_options = {
     class: 'selectbooktitle form-control', 
     'data-change-form-action-with-value': true,
  } %>

  <%= form.collection_select :post_path, Post.all, method(:post_path), :title, options, html_options %>
  <%= form.submit 'Search', class: 'btn btn-outline-success'%>
<% end %>

app/assets/javascripts/some_file.coffee

initChangeFormActionWithValue = (selectElement) ->
  selectElement       = $(selectElement)
  closestAncestorForm = selectElement.closest('form')

  selectElement.on 'change', ->
    closestAncestorForm.attr 'action', selectElement.val()

$(document).on 'turbolinks:load', ->
  $('select[data-change-form-action-with-value="true"]')
    .each -> initChangeFormActionWithValue(this)

现在,当更改选择值时,表单会更新为所选选项的值(包含发布路径)。当您提交请求时,您将请求GET /posts/:id

注意:这是一个简化的解决方案。哪个请求所选帖子的GET /posts/:id。然而,表单的值也会被提交。您可以不处理它们,但至少应该知道它们在那里。因此,当您在节目中看到 post_pathsubmit 参数时,您不会感到惊讶。

此外,如果您在按下提交时根本不更改选择,您目前只需请求GET /posts。如果您将选择值更改为帖子并返回到'Post titles'(空白值),则该操作将被清除。这意味着您将获得当前 URL。


或者,您可以使用当前的解决方案。如果params[:post_id] 存在,则在您的PostsController#index 重定向到PostsController#show

class PostsController < ApplicationController

  def index
    return redirect_to post_path(params[:post_id]) if params[:post_id]

    # other index code
  end

end

【讨论】:

  • 非常感谢您的详细回答!我看到了我现在正在经历的问题并且能够解决它。基本上,您的 JavaScript 示例对我来说看起来很合理——但我仍然选择了您的替代解决方案,因为它只适用于我尝试过的方式。你在这里有我的赞成票和最好的答案!
猜你喜欢
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多