如何使用带有link_to - rails 3的params调用方法

时间:2013-02-02 21:24:13

标签: ruby-on-rails ruby ruby-on-rails-3 link-to

我有一个名为School的模型,我有一个名为custom_search的方法:

def self.custom_search( text, page, order = :asc )
    search( include: [:prices] ) do 
      fulltext text
      paginate page: page, per_page: 7
      order_by :price, order
    end
end

如何从带有这样的参数的视图中调用它:

School.custom_search params[:search], params[:page]
# or :
School.custom_search params[:search], params[:page], :desc

我是否使用link_to方法?

1 个答案:

答案 0 :(得分:0)

# app/controllers/schools_controller.rb
def search
  @schools = School.custom_search params[:search], params[:page]
end

# in your view
link_to 'My link', search_schools_path(search: 'Bar', page: 1)

# config/routes.rb
resources :schools do
  collection do
    get 'search'
  end
end