Rails will_paginate 自定义路由 [英] Rails will_paginate custom route
问题描述
如何将 will_paginate 与自定义路由一起使用?
How can I use will_paginate with a custom route?
我的路线中有以下内容:
I have the following in my routes:
map.connect 'human-readable/:name', :controller => :tags, :action => 'show'
但就我所知,will_paginate 使用 url_for,但我想使用人类可读"而不是 url_for,但如何使用?
but will_paginate uses url_for as far as I can tell, but I want to use 'human-readable' instead of url_for, but how?
编辑
当我点击由 will_paginate
生成的分页链接时,它正在使用:
When I click the paging link generated by will_paginate
, it's using:
"tags/show?name=Elektronikindustri&page=1"
代替:
"/human-readable/show?name=Elektronikindustri&page=1"
我希望 will_paginate
使用我的自定义路由而不是实际的控制器名称
I want will_paginate
to use my custom route instead of the actual controller name
推荐答案
will_paginate
视图助手有一个 :params
选项,用于覆盖默认链接生成.
The will_paginate
view helper has a :params
option for overriding the default link generation.
更改您的路由配置:
map.human_readable_tag '/human-readable/:name',
:controller => :tags, :action => 'show'
调用 will_paginate
视图助手如下:
Invoke the will_paginate
view helper as follows:
<%= will_paginate @tag_list,
:params => {:controller => human_readable_tag_path(@tag_name) } %>
确保您已在控制器中设置了 @tag_name
变量.
Make sure you have set the @tag_name
variable in your controller.
欲了解更多信息,请阅读 查看帮助文档.
For more information read the will_paginate
view helper documentation.
传递给帮助程序的 :params
选项用于调用 url_for
.因此,请阅读 url_for
文档,了解我们如何伪造控制器名称.
The :params
option passed to the helper is used to invoke url_for
. So read the url_for
documentation for how we faked a controller name.
这篇关于Rails will_paginate 自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!