在 rails 中 will_paginate 的 page_entries_info 中提供自定义消息 [英] Give custom messages in page_entries_info of will_paginate in rails

查看:36
本文介绍了在 rails 中 will_paginate 的 page_entries_info 中提供自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手.我想为 page_entries_info 显示我的自定义消息.我已经浏览了以下链接,但不能理解太多.谁能详细解释一下.

I am new to rails. I want to display my custom message for page_entries_info. I have gone through following link but cannot understand much. Can anyone please explain in details.

how-do-i-specify-custom-wording-in-a-will-paginate-view-helper

推荐答案

另一个选项是您可以在 ApplicationHelper 中定义您的 page_entries_info() 方法并像往常一样使用它.如果您知道不需要覆盖边缘情况(如我的情况),这将为您提供更大的灵活性,甚至可以更加清洁和高效.您可以参考原始方法定义这里 看看有什么所有你需要实施.以下代码将运行您的大部分问题!

Another option is you can define your page_entries_info() method in your ApplicationHelper and use it as you normally would. This would give you more flexibility and can even be more cleaner and efficient if you know that you dont need to cover the edge cases (as in my case). You can refer the original method definition here and see what all you need to implement. Following code would run for most part of your problem!

def page_entries_info(collection, options = {})
  entry_name = options[:entry_name] || (collection.empty?? 'item' :
      collection.first.class.name.split('::').last.titleize)
  if collection.total_pages < 2
    case collection.size
    when 0; "No #{entry_name.pluralize} found"
    else; "Displaying all #{entry_name.pluralize}"
    end
  else
    %{Displaying %d - %d of %d #{entry_name.pluralize}} % [
      collection.offset + 1,
      collection.offset + collection.length,
      collection.total_entries
    ]
  end
end

这篇关于在 rails 中 will_paginate 的 page_entries_info 中提供自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆