问题
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