简体   繁体   English

Ruby on Rails:使用pg_search和will_paginate在搜索后转到下一页时出现404找不到错误

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

I'm facing a strange error, where going to next page after search will cause 404 not found error. 我遇到一个奇怪的错误,在搜索后转到下一页将导致404 not found错误。 But the pagination works well if I don't search. 但是,如果我不搜索,分页效果很好。

routes.rb 的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 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 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

error message 错误信息

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)

any one can help please, I still really don't know what is going on with that error after a few hours of research 任何人都可以帮忙,经过几个小时的研究,我仍然真的不知道该错误是怎么回事

When you search the parameters are submitted via the POST method ( method: :post ). 搜索时,将通过POST方法( method: :post )提交参数。 When you click next page you request the page via GET, however the browser does not send the search parameters via GET and there is no route get :search so the show action is used instead leading to the 404 page. 当您单击下一页时,您是通过GET请求该页面的,但是浏览器不会通过GET发送搜索参数,并且没有路由get :search因此使用了show动作来转至404页面。

While it may be possible to make will_paginate use POST it is not recommended. 虽然它可能是能够使will_paginate使用POST不推荐。

Cf. 参看 https://github.com/mislav/will_paginate/wiki/Simple-search : https://github.com/mislav/will_paginate/wiki/Simple-search

When you create a search form, make sure its method is GET 创建搜索表单时,请确保其方法为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. 如果您的表单操作是POST(或任何其他HTTP方法),则搜索词将不会应用于后续的页面链接。 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. 无论如何,使用GET发布的搜索表单是更好的做法。 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. 缓存是另一个好处:浏览器,代理等乐于缓存由于GET请求而产生的内容。

帖子:search,放在::collection但您的请求得到了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM