January 31, 2006 – Ruby on Rails Dates
Is it sad that I noticed that my sites were down only when I stopped receiving LJ automated replies to comments? Or is it worse that ArcadeFly.com expired 3 days ago and I forgot to renew it. (I renewed it now, luckily there’s a few days grace period). Local development/experimentation with Rails is going pretty well. With the beauty of the framework and language it’s painful to cut corners, so I’m trying to learn to do everything the right way from the start. Means a little bit slower development, but in the end it’ll hopefully mean a cooler, easier to mantain site. For the hell of it i’ll post some basic rails code to show how easy it is, and why i’m so amazed at how much easier it is to do stuff in rails vs php.
So i’m adding a little date of birth field for when users register. On the form I add this little bit of text:
<%= date_select("register", "dob") %>
Code language: JavaScript (javascript)
Pretty simple right? That’ll create 3 form fields, a month, date and year select box with form names register[dob(1i)],register[dob(2i)]
, and register[dob(3i)]
. The month names will be in english. The year will unfortunately be limited to +/- 5 years from today, and it’ll default to january 1,2006. Also it won’t show up in month/day/year order like we’re familiar with but day/month/year. That can be easily changed though…
<%= date_select("register", "dob", :start_year => 1900, :end_year => 2006, :order =>; [:month,:day,:year], :include_blank => true) %>
Code language: JavaScript (javascript)
So is it obvious what the newly added attributes mean? 🙂 That’s one cool thing about ruby/rails, it needs almost no comments in your code. Usually for these fields you’d have to create a select box for each, looping over 1-12, 1-31, 1900-2006 for their respective fields. So what’s the backend look like for processing this?
def create
@user = User.new(params[:register])
if @user.save
flash[:notice] = 'User was successfully created.'
redirect_to :controller => 'account'
else
render :action => 'register'
end
end
Code language: PHP (php)
So what’s this doing? Assuming for the form submitted to the “create” action (with our form being on the “register” action, although it could come from anywhere) this block of code executes. It declares a variable @user to be a new user created with the register[]
structure from the form (which would contain all fields such as username, password, etc). You’ll notice dob isn’t anywhere in here, it’s just part of the params[:register]
variable. Pretty slick not needing to handle all the variables individually. The next part is pretty slick as well — “@user.save”. This attempts to do an insert into the database of the entire user object, and based on the model (not shown here) and the structure of the database will either be true or false. If it is rails saves a string of text in the session scope (using “flash”) and which can show up on whatever page is accessed next. In this case it redirects to the account page where it will show thay the user was created. If user creation fails, rails is nice enough to generate an error object with everything that went wrong which can be used when the form is recreated (when it’s sent back to the ‘register’ action).
I’m messing around with some ajax stuff for fun now, so this code isn’t actually what i’m using, but you can get an idea of how easy it is. Notice that there’s no validation (thats handled in the model), sql (that’s just skipped), and no HTML (well that’s handled in the partials, but that’s another story). Error messages, what to say when a field is entered incorrectly is handled in the model as well, so no matter what page you’re on if you’re updating/creating a user you’ll get the same validation.
Anyways, it was just amazing me how easy it was to do some things, so I thought I’d share. To add icing to the cake, when the page is regenerated the 2nd time (if there were errors), the fields with errors that don’t validate are in a div with class="fieldwitherrors"
attribute to keep things easy. Quite a lot of code saved.
Let's keep in touch 🧑🤝🧑
- Send me an email at [email protected]
- Follow me on Mastodon at @[email protected]
- Subscribe to my monthly newsletter
- Subscribe to my RSS feed
Did you link to this article? Add it here and I'll include it