user592638
user592638

Reputation:

How to call method with params with link_to - rails 3

I have a model named School and I have a method called 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

How do I call it from a view with params like this:

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

Do I use the link_to method?

Upvotes: 0

Views: 851

Answers (1)

mathieugagne
mathieugagne

Reputation: 2495

# 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

Upvotes: 1

Related Questions