开发者

Using one search form to search multiple models

I am working a simple rails app and i would like to know how possible it is to use one search form to search inside multiple models. like i have a story model and a book model. this search form should be able to search the both models with a single parameter.开发者_JS百科

<%= for_tag :url => search_path %> <%= text_field_tag :q %> <% end %>

How can i make this search from work for multipple models


Whatever search you need to do, is done inside an action in a controller. You could basically create a controller, say search_controller and have an action say, item

def item

   if params[:q]
       @found_stories = Story.find_all_by_...(params[:q])
       @found_books= Book.find_all_by_...(params[:q])
   end

end

Then you could use the objects @found_stories and @found_books in your view to show the search results.

This is just an example of how you could do to fulfill your requirement.

Thanks.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜