Ruby on Rails:在搜索后转到下一页时使用pg_search和will_paginate获取404未找到错误

时间:2018-04-16 03:59:03

标签: ruby-on-rails will-paginate pg-search

我面临一个奇怪的错误,搜索后转到下一页会导致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" = $1 LIMIT $2  [["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)

任何人都可以提供帮助,经过几个小时的研究后,我仍然不知道该错误发生了什么

2 个答案:

答案 0 :(得分:1)

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

虽然可能possible使will_paginate使用POST,但不建议这样做。

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

  

创建搜索表单时,确保其方法为GET

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

如果您的表单操作是POST(或任何其他HTTP方法),那么   搜索字词不会应用于后续页面链接。换一种说法,   当你按照&#34;下一页&#34;或任何其他页面链接,搜索   参数将会丢失。

     

无论如何,使用GET发布的搜索表单都是更好的做法。之一   直接的好处是用户能够为搜索结果添加书签。   缓存是另一个好处:浏览器,代理等很乐意缓存   由GET请求导致的内容。

答案 1 :(得分:0)

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