开发者

Should I use cookies, session values, or hidden fields to store data items that need to be persisted between requests?

开发者 https://www.devze.com 2023-01-30 05:13 出处:网络
This is a Rails 3 project. Am I abusing the use of cookies if I store query values there? I have a dataset that a user can \"drill-down\" through, so as the user clicks through the data, he amasses a

This is a Rails 3 project.

Am I abusing the use of cookies if I store query values there? I have a dataset that a user can "drill-down" through, so as the user clicks through the data, he amasses a bunch of query values that further limit the data presented on the next request.

Right now I'm doing this with 开发者_StackOverflowa cookie, and it works great, except that I can't figure out to check to see if cookies are enabled. So some people using IE are giving me fits because the app just fails with no errors.

I used to put values like this in a session variable, which worked great until it mysteriously didn't, i.e. when memcached aged or cleared them out. I wouldn't want to keep the values in a session in the db because I don't want the extra hits on every request.

So I suppose I could put the values either in hidden form fields, or append them to the links on the page that I'm presenting each time. Is there a conventional Rails Way to do this that I'm missing?


If you're showing a different set of results, the URL should reflect this. This makes URL query parameters the natural choice. This provides several benefits:

  • There is no state at all. You don't have to store anything or break the stateless nature of HTTP.
  • There is a one-to-one correspondence between sets of query results and URLs.
  • You can link to query results.
  • Works on everything, ever.
0

精彩评论

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