开发者

Rails 3 - Passing Parameter into search Model for Nested Searching

开发者 https://www.devze.com 2023-03-20 13:56 出处:网络
I am using a nested search resource in my Rails 3 Application where I am following Ryan Bates Railscast on Advanced Searching. The problem Arises however when I call Will_Paginate on my Search Model b

I am using a nested search resource in my Rails 3 Application where I am following Ryan Bates Railscast on Advanced Searching. The problem Arises however when I call Will_Paginate on my Search Model because obviously it requires a page parameter to be passed into it. I Grab the page parameter in my Search con开发者_运维知识库troller, but how Can I inject this into my model for will_paginate to work Properly?? (I need to inject it into my model as this is where the database query is run for parameters passed in)

For anyone who thinks this has already been answered, I do apologise and have not seen a relevent question Myself!

Thanks.


in your model:

def search(to_search, page)
  unless to_search.blank?
    where("field = ?",to_search).paginate(:per_page =>20, :page=> page)
  else
    paginate(:per_page =>20, :page=> page)
  end
end

in your controller

@things = Thing.search(params[:search],params[:page])

you mention nested model in your title but not in the OP but the logic is the same just pass it as a parameter to a function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号