开发者

Rails - storing search query/result

I have a search page which finds candidates. From this page you can click view to find more information about the candidate. When on the candidate view yo开发者_如何学Cu can click edit or do a number of other actions which would return you too the candidates view.

My problem is from the candidates view I need to add a button to go back to the search results. I originally thought of using a JS button with history -1 but because the user can do other action from inside the view this won't work.

I am still quite new to rails so not sure of my options... I am thinking some sort of caching of the results and then maybe a hidden field to keep track of the location of the cache(don't think this is the best solution as keeping track of the hidden value could get abit messy!)

Thanks, Alex


I would probably use a session variable to store this information.

First, make sure your form that posts to the search page is a GET operation, this way the search details are in your query string. Then in your search action, you can grab the request URL and store it in the session:

session[:search_results] = request.url

Now in your view for the results, you can do your "Back to search results" like this:

link_to "Back to search results", session[:search_results]


You have a couple of options:

Cache the results, as you've suggested. The potential downsides to this are that it takes memory, and if new valid records get added, you won't see them. You could store the cache in Session, or in the database (though in the latter case, you don't gain much).

I'd suggest just remembering the last search term, either in session or using hidden fields. You end up re-running the query when you go to the search results page, but in a properly indexed DB, that shouldn't be a big deal.

Good luck!


You can include the parameters for the query on the subpage. Eg.: /foo/search?q=stuff displays search result. Each result then has a link like /foo/:id?q=stuff. And on the subpage, you will have the parameter available to link back to the main page.

This solution doesn't use any server side state, which is generally accepted as the better way to build web applications. Not only does it mean that you browser will behave as expected, with respect to bookmarks, multiple tabs etc., but it also ensures that proper caching can be employed. Further, it lowers the complexity of your application, making it easier to debug and extend.


You could put the search results in a "search_results" table keyed by the user id. Then when the user hits the page, always load from a query on that table.


If anybody does come across this page and you need a button that goes back to the previous page and still display those search results (just like the google chrome button), just use :back.....

<%= link_to(image_tag("back.svg"), :back, :class => 'back_btn') %>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜