简体   繁体   English

Rails将_paginate自定义路由

[英]Rails will_paginate custom route

How can I use will_paginate with a custom route? 如何将will_paginate与自定义路由一起使用?

I have the following in my routes: 我的路线中有以下内容:

map.connect 'human-readable/:name', :controller => :tags, :action => 'show'

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会使用url_for,但我想使用'human-readable'而不是url_for,但是怎么样?

Edit 编辑

When I click the paging link generated by will_paginate , it's using: 当我点击will_paginate生成的分页链接时,它正在使用:

"tags/show?name=Elektronikindustri&page=1"

Instead of: 代替:

"/human-readable/show?name=Elektronikindustri&page=1"

I want will_paginate to use my custom route instead of the actual controller name 我希望will_paginate使用我的自定义路由而不是实际的控制器名称

The will_paginate view helper has a :params option for overriding the default link generation. will_paginate视图助手有一个:params选项,用于覆盖默认链接生成。

Change your routes configuration: 更改路线配置:

map.human_readable_tag '/human-readable/:name', 
     :controller => :tags, :action => 'show'

Invoke the will_paginate view helper as follows: 调用will_paginate视图助手,如下所示:

<%= will_paginate @tag_list, 
     :params => {:controller => human_readable_tag_path(@tag_name) } %>

Make sure you have set the @tag_name variable in your controller. 确保在控制器中设置了@tag_name变量。

For more information read the will_paginate view helper documentation . 有关更多信息,请阅读will_paginate视图帮助文档

The :params option passed to the helper is used to invoke url_for . 传递给助手的:params选项用于调用url_for So read the url_for documentation for how we faked a controller name. 因此,请阅读url_for 文档 ,了解我们如何伪造控制器名称。

Another option is to use param in will_paginate but passing in the parameter like so: 另一个选择是在will_paginate中使用param但是传入参数如下:

<%= will_paginate @products, :params => {:controller => 'human-readable', :action => 'show', :name => 'xyz'} %>

and now your links will look like human-readable/xyz?page=2... 现在你的链接看起来像人类可读/ xyz?page = 2 ...

You need define this route too 您也需要定义此路线


map.connect 'human-readable/:name', :controller => :tags, :action => 'show'
map.connect 'human-readable/:name/page/:page', :controller => :tags, :action => 'show'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM