ruby rails will_paginate 更改默认值 link
ruby on rails will_paginate change default link
在 routes.rb
中,我将 lists#index 设置为根:
root to: "lists#index"
我想 will_paginate
创建指向 /?page=#
而不是 /lists?page=#
的页面 href
s。要创建分页链接,我在 "lists#index" 文件中有以下行:
<%= will_paginate @lists %>
我从你的问题中了解到你正在尝试创建一个自定义渲染器,虽然我从未使用过它,但为此你需要重写渲染器的 link 方法。
这是原始代码的 link 可能会有一些帮助 -
https://github.com/voormedia/paginary/blob/master/lib/paginary/helpers/pagination_helper.rb
解决方案确实是自定义渲染器。谢谢阿尤什!
下面帮我解决了: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
。
在这种特殊情况下,链接受 routes.rb 文件中路由指令顺序的影响。
如果你把线
root to: "lists#index"
在 routes.rb 的顶部,然后 will_paginate
将生成指向 /?page=#
的链接,而无需自定义渲染器。
这可以在 coursera 模块的常见问题解答中找到。
在 routes.rb
中,我将 lists#index 设置为根:
root to: "lists#index"
我想 will_paginate
创建指向 /?page=#
而不是 /lists?page=#
的页面 href
s。要创建分页链接,我在 "lists#index" 文件中有以下行:
<%= will_paginate @lists %>
我从你的问题中了解到你正在尝试创建一个自定义渲染器,虽然我从未使用过它,但为此你需要重写渲染器的 link 方法。 这是原始代码的 link 可能会有一些帮助 - https://github.com/voormedia/paginary/blob/master/lib/paginary/helpers/pagination_helper.rb
解决方案确实是自定义渲染器。谢谢阿尤什!
下面帮我解决了: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
。
在这种特殊情况下,链接受 routes.rb 文件中路由指令顺序的影响。
如果你把线
root to: "lists#index"
在 routes.rb 的顶部,然后 will_paginate
将生成指向 /?page=#
的链接,而无需自定义渲染器。
这可以在 coursera 模块的常见问题解答中找到。