Ruby on Rails select2提交不起作用

时间:2018-05-09 10:42:13

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

  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)

导致此网址:

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

它应该指向以下网址:

http://localhost:3000/posts/20

我做错了什么?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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

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

这是一个简化的例子:

应用/视图/ 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 %>

应用/资产/ Javascript角/ 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_path 提交参数时,您并不感到惊讶。

     

此外,如果您在按提交时根本没有更改选择,那么您目前只需要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