How best to DRY will_paginate options

后端 未结 2 1744
误落风尘
误落风尘 2021-02-15 05:34

I have 8 controllers using will_paginate to paginate their index pages. I\'d like to override the defaults for \"Previous\" and \"Next\" on each without having to specify the s

2条回答
  •  悲&欢浪女
    2021-02-15 05:59

    I'm assuming you're doing something like this in your controllers:

    will_paginate @collection, :previous_label => '< go back', :next_label => 'go forward >'
    

    Your problem is that you want to use these labels everywhere in your application, so it's pointless to repeat them. In that case, you could define a helper like this:

    def paginate(collection, options = {})
      defaults = {
        :previous_label => '< go back',
        :next_label     => 'go forward >',
      }
      options = defaults.merge(options)
    
      will_paginate collection, options
    end
    

    After that, calling paginate @collection in your views will use your defaults and still let you override them if necessary.

    EDIT: suweller's response is definitely the better way to go in this case, especially considering it's approved by mislav, the plugin creator :). I'd completely forgotten about the translation file option. My solution could probably be useful in the general case, when the helper is not configurable in a similar way.

提交回复
热议问题