Rails 上的 Ruby:搜索后转到下一页时出现 404 未找到错误,使用 pg_search 和 will_paginate

Ruby on Rails: getting 404 not found error, when going to next page after search, using pg_search and will_paginate

我遇到了一个奇怪的错误,搜索后转到下一页会导致 404 未找到错误。但是如果我不搜索,分页效果很好。

routes.rb

resources :activities do
  post :search, on: :collection
end

match "/403", to: "errors#error_403", via: :all
match "/404", to: "errors#error_404", via: :all
match "/422", to: "errors#error_422", via: :all
match "/500", to: "errors#error_500", via: :all

get :ie_warning, to: 'errors#ie_warning'
get :javascript_warning, to: 'errors#javascript_warning'
get :pwreset, to: "pages#pwreset"
get :activities, to: "activities#index"
get :home, to: "pages#home"
root to: "pages#home"

controller.rb

@@page_size = 3
# GET /activities
def index
  @activities = Activity.paginate(:page => params[:page], :per_page => @@page_size)
end

# POST /products/search
def search
  if params[:search][:title].present?
    @activities = Activity.search_activities(params[:search][:title]).paginate(:page => params[:page], :per_page => @@page_size)
    render :index
  else
    @activities = Activity.paginate(:page => params[:page], :per_page => @@page_size)
  end

end

html.haml

- @activities.each do |activity|
.row
  .col-md-8
    = link_to activity.title, activity
  .col-md-2
    %p= activity.publisher
  .col-md-2
    %p= activity.created_at.to_date

.force-to-bottom
  .text-center
    = will_paginate @activities


= simple_form_for :search, url: search_activities_path, method: :post do |f|

  .form-inline
    = f.input :title, label: false, placeholder: 'search...'

    = button_tag(type: 'submit', class: "btn btn-primary") do
      %span.glyphicon.glyphicon-search{"aria-hidden" => "true"}

    %span.glyphicon.glyphicon-remove{"aria-hidden" => "true"}
    = link_to 'clear filter', activities_path

错误信息

Started GET "/activities/search?page=2" for ::1 at 2018-04-16 04:39:57 +0100
Processing by ActivitiesController#show as HTML
Parameters: {"page"=>"2", "id"=>"search"}
Activity Load (17.9ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" =  LIMIT   [["id", 0], ["LIMIT", 1]]
Rendering errors/error_404.html.haml within layouts/application
Haml::TempleEngine: Option :ugly is invalid
Rendered errors/error_404.html.haml within layouts/application (6.2ms)
Haml::TempleEngine: Option :ugly is invalid
Haml::TempleEngine: Option :ugly is invalid
Rendered layouts/_environment_notice.html.haml (2.0ms)
Completed 404 Not Found in 245ms (Views: 162.2ms | ActiveRecord: 74.7ms)

任何人都可以帮忙,经过几个小时的研究,我仍然真的不知道那个错误是怎么回事

post :search, on: :collection 但是你的要求是get?

当您搜索时,参数是通过 POST 方法 (method: :post) 提交的。当您单击下一页时,您通过 GET 请求页面,但是浏览器不会通过 GET 发送搜索参数并且没有路由 get :search 因此使用 show 操作而不是导致 404 页面。

虽然 possible 使 will_paginate 使用 POST 但不推荐。

比照。 https://github.com/mislav/will_paginate/wiki/Simple-search :

When you create a search form, make sure its method is GET

<% form_tag request.path, :method => 'get' do %>
  <% content_tag :label do %>
    Search term:
    <%= text_field_tag :search, params[:search] %>
  <% end %>
<% end %>

If your form's action is POST (or any other HTTP method) then the search term won't be applied to subsequent page links. In other words, when you follow the "Next page" or any other page link, the search parameter(s) would be lost.

Search forms that post with GET are better practice, anyway. One of immediate benefits is that users are able to bookmark search results. Caching is another benefit: browsers, proxies, etc. are happy to cache content that was a result of a GET request.