Wrong url in paginate (kaminari) for search with %

自古美人都是妖i 提交于 2019-12-13 03:56:34

问题


I have paginate (kaminari) on search page, and if i search somthing with % like "50% discount" i get page http://some.domain.com/50%25+discount where paginate has wrong urls (without escaping %) like: http://some.domain.com/50%+discount?page=2

Do i do somthing wrong? Or it's bug in gem?

Thank you


回答1:


% is a special character in urls isn't it? Your going to need your search method to sanitize the query before it gets passed to the url.

I believe you can use the methods for the String class provided in the gem Stringex to sanitize the search term.

https://github.com/rsl/stringex

From there github page.

"10% off if you act now".to_url => "10-percent-off-if-you-act-now"

Edit:

You would need to have something like this (This isn't very clean but it gives you an idea)

class SearchesController < ApplicationController
  def new
     #Form in its view that goes to create via json
  end

  def create
     query = params[:query].to_url
     redirect_to "/search/#{query}"
  end

  def show
    #search paged on params[:query]
  end

end

routes

resources :searches, :only => [:new, :create, :show], :new => ""
get "/search" => "searches#new", :via => :get

You would be acting like your treating it like a normal object but your never actually saving it. You could even change the name of the :create method to parse if you want, but this way the built in rails helpers and logic will work.




回答2:


My solutions is

paginate @entities, :params => { :keyword => CGI.escape(@keyword) }

in route.rb i have

match "/:keyword" => "route#index", :keyword => /.*/



来源:https://stackoverflow.com/questions/13994710/wrong-url-in-paginate-kaminari-for-search-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!