rails 3:将分页创建不正确的链接

This is my route

match "/search/*keywords" => "public/search#index" ,:method=>:get,    :as=>:search_with_keywords    

an example could be "/search/country-india/product_type-mobilephones"

am using will paginate to create pagination links
the */keywords is causing the following problem while creating the pagination link

<%= will_paginate @results, :container => false, :class => 'pagination ajax' %>  

the generated link is "http://localhost:3000/search?keywords=country-india%2Fproduct_type-mobilephones&lang=en&page=2"

what i want is "http://localhost:3000/search/country-india/product_type-mobilephones?lang=en&page=2"

how can i achieve this ...

#0

If you're using the Rails 3 version of Will Paginate (currently "3.0.pre2") then you can use the :renderer option and customize how the links are generated.

So first, if you're not up-to-date then you can follow the installation instructions and usage instructions from the will paginate wiki.

Then in lib you can create your own renderer that overwrites the link generation, perhaps something like

class SearchLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer

  protected

  def url(page)

    @base_url_params ||= begin
      url_params = base_url_params
      merge_optional_params(url_params)
      url_params
    end

    url_params = @base_url_params.dup
    add_current_page_param(url_params, page)

    @template.search_with_keywords_url(url_params)
  end
end

and then call it with

<%= will_paginate @results, :container => false, :class => 'pagination ajax', :renderer => SearchLinkRenderer %>

The default renderer uses

@template.url_for(url_params)

which is what is "messing up" your links. You can use any path from @template, so

@template.search_with_keywords_url(url_params)

should do the trick. Now if you call will_paginate with your own renderer, it should pass use your path instead of url_for, and the parameters should match up nicely. If they don't then check what url_params is being set to and compare it with your route matchers.

There might be an easier way to overwrite this, but this seemed pretty straightforward and consistent with how will_paginate expects you to customize its output. I found that Rob Anderton has a great article on customizing will_paginate here, so maybe they can offer more insight.

#1

You might also be interested in using Kaminari as an alternative to will_paginate for Rails 3. I discovered it via Railscast and was able to quickly and efficiently integrate it into my app. I also found my app's size decreased and performance increased noticeably after the switch.

推荐文章

在项目之间共享变量

在项目之间共享变量

推荐文章

如何为单元测试手动创建测试资源文件夹

如何为单元测试手动创建测试资源文件夹

推荐文章

有可插拔的在线python控制台吗?

有可插拔的在线python控制台吗?

推荐文章

固定屏幕方向

固定屏幕方向

推荐文章

生成子进程并通过套接字连接的正确方法

生成子进程并通过套接字连接的正确方法

推荐文章

Java应用程序部署/安装选项?

Java应用程序部署/安装选项?

推荐文章

如何在Eclipse中使用(共享)项目范围的拼写检查字典?

如何在Eclipse中使用(共享)项目范围的拼写检查字典?

推荐文章

使用malloc会导致错误

使用malloc会导致错误

推荐文章

将android open GL代码移植到iPhone

将android open GL代码移植到iPhone

推荐文章

如果我不为子类定义构造函数,我可以直接使用超类的构造函数吗?

如果我不为子类定义构造函数,我可以直接使用超类的构造函数吗?

推荐文章

我可以访问其他用户使用的相同静态方法吗?

我可以访问其他用户使用的相同静态方法吗?

推荐文章

Window.location.href在IE8中播放

Window.location.href在IE8中播放

推荐文章

基于两列对大文件排序

基于两列对大文件排序

推荐文章

具有从服务器到客户端的查询的持久连接

具有从服务器到客户端的查询的持久连接

推荐文章

根据使用javascript在另一个文本框中输入的值自动在文本框中显示值

根据使用javascript在另一个文本框中输入的值自动在文本框中显示值

推荐文章

谷歌电视模拟器没有chrome浏览器?

谷歌电视模拟器没有chrome浏览器?