gpt4 book ai didi

ruby-on-rails - ruby on rails will_paginate 更改默认链接

转载 作者:行者123 更新时间:2023-12-02 17:22:26 26 4
gpt4 key购买 nike

routes.rb我将 lists#index 设置为根:

root to: "lists#index"

我想要will_paginate创建页面 href s 指向 /?page=#而不是 /lists?page=# .要创建分页链接,我在“lists#index”文件中有以下行:

<%= will_paginate @lists %>

最佳答案

解决方案确实是自定义渲染器。谢谢阿尤什!

以下帮助我解决了它:how-to-customize-the-will-paginate-links-with-images

我创建了一个文件:app/helpers/will_paginate_helper.rb,内容如下:

module WillPaginateHelper
class RootedLinkRenderer < WillPaginate::ActionView::LinkRenderer
protected
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = ""
target = "/?page=#{target}"
end
attributes[:href] = target
tag(:a, text, attributes)
end
end
end

最后,我更新了索引文件并替换为:

<%= will_paginate @lists %>

与:

<%= will_paginate @lists, renderer: WillPaginateHelper::RootedLinkRenderer %>

所以 href 是 http://localhost:3000/?page=2 而不是 http://localhost:3000/lists?page=2

关于ruby-on-rails - ruby on rails will_paginate 更改默认链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41717522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com