Wrong url in paginate (kaminari) for search with %

2019-08-30 09:42发布

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

2条回答
可以哭但决不认输i
2楼-- · 2019-08-30 10:23

My solutions is

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

in route.rb i have

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

查看更多
一夜七次
3楼-- · 2019-08-30 10:43

% 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.

查看更多
登录 后发表回答