I have the following (heavily simplified) model, that uses will_paginate
class Search < ActiveRecord::Base
  attr_reader :articles
  def after_initialize
    @articles = Article.paginate_by_name name, :page =&开发者_开发百科gt; 1
  end
end
and the controller code in my show action is
@search = Search.new(params[:search])
This all works fine, but notice i hard coded the page number to 1, problem is passing params[:page] value into the after_initialize method, can anyone suggest an elegant way to do this please?
Thanks
Add a page parameter (or even better an options hash parameter) to the initialize method:
class Search
  def initialize(search, options = {})
    @options = options
  end
  def after_initialize
    @articles = Article.paginate_by_name name, :page => @options[:page]
  end
end
and then in your controller:
@search = Search.new(params[:search], :page => params[:page])
You can even supply default values to the option hash, if you like.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论