Latest Tweet
- Downgraded to iPhone OS 3.1.3, upgraded to responsive 3G iPhone. :P 2 weeks ago
- More updates...
Categories
Tags
academia alcohol animals boyfriend cooking databases email forwards family Flickr food friends health Lexington Linux list Mario math movies music news OS X Perl photos PHP politics programming quizzes Rails rants reading Ruby screenshots shopping Sims sports themes tutorials Twilight is ridiculous vehicles video games videos weather Web development work zombies again-
Recent Comments
-
Random Quote
Who controls the past controls the future; who controls the present controls the past.
— George Orwell Syndication
All posts RSS feed
simple Rails preference storage
So you’ve got some Rails application and you need to store information from the users across their interactions with the app. Here’s a simple, straightforward way to do that.
In your controller:
This checks to see if ‘option’ was passed via a parameter in the URI (e.g. /view?option=jim) or from a form. If it is, then we want to save that option in both the session, so we can access it later, as well as in a variable specifically for use in our view.
However, if
params[:option]is nil but we still have it in session[:option] from some previous interaction, then we’ll set our variable@optionfor use in our view from the session copy.If all else fails, then we know the user hasn’t tried to set this option in any way, and we can give it some default value.
Now, in our view:
This provides a simple form for our user to set the value of this option, and if the value is already set from a previous interaction with this form, then it shows up in the text field.