Tuesday, May 24, 2011

Boolean behavior

So I finally found a (pretty good) tutorial to build the To-Do list application. This one is still a couple years out of date, but I was able to figure out how to do most of what it describes.

This tutorial also mentions the nifty-generators gem, which just applies some basic CSS so you don't just end up with the standard black and white view. It's neat, but I'll still end up doing most of my own styling on more finished applications. (CSS is, after all, my first love)

However, I ran into a problem when trying to get rails to generate a nifty-layout. The solution was to manually add nifty-generators to the gemfile list. Easy enough!

So I ended up with three columns: name:string, description:text and finished:boolean. I've never used a boolean field before, and I did not find the standard issue true/false states satisfactory. Another google search gave me the code to edit this to a more user-friendly yes/no.

This was simply a matter of updating the view. I went from this:

  <%= @task.finished? %>

To this:

  <%= @task.finished? ? 'Yes' : 'No' %>

Aww shit, who is the dude so rad he uses a ternary operator? (Side note: I should probably brush up some more on my Ruby. But Rails is SO HOT right now!) I also updated this in the edit file, mostly for consistency.

However, I still want to be able to check or uncheck a task as "Finished," and have that change reflected in the database and displayed on the view, as well. I'm sure this is easy enough to do, but I haven't found quite what I'm looking for yet. I haven't been able to convert the old code the tutorial provides into code that makes more sense to me yet.

I also added some validation to the Tasks model. First, a task must at least have a name, right? Description should be optional, so I won't require that. How much description does "Take out the trash" really need? I also think a task is unique, so I am validating for that, as well. I also figured it makes sense for the name and description to have character limits, mostly to keep the view tidier. And here's my Task model:

class Task < ActiveRecord::Base   validates_presence_of :name   validates_uniqueness_of :name   validates_length_of :name, :maximum=>30
  validates_length_of :description, :maximum=>100

And that's about it. I spent about an hour working on this, of which about 15 minutes was generating working code, 15 minutes was googling for more information and 30 minutes was spent trying to figure out how to get that boolean field to behave.

Sunday, May 22, 2011

Everybody's coding on the weekend

So here's what I've been up to today:


I like doing tutorials and I always learn something new from each one, but there are some annoying factors. For one thing, since Rails has been around for a while now, a lot of the tutorials I find that I want to create are for older versions of Rails. I'm still interested in doing these, but figuring out what the new syntax is for older examples is tricky. I found an updated version for the recipe application and finished that up last night. Now I'm trying to find a new version for a to-do list.

I tried a couple of video tutorials, but the problem with those is I like to code along with the tutorial, and it's a pain in the ass to constantly pause and restart a video. Still, railscasts is a great resource and it would be worthwhile just to watch, even if I'm not coding along with the video.

One final gripe: a lot of Rails tutorials are geared towards beginners, so every one walks me through what Rails is, a brief history of Rails, how to install Rails and so on. I get it, and it's useful, but it gets tedious. I'd like to see more tutorials geared for slightly more experienced users. Do such things exist? I don't know!

Still, doing just a fucking ton of tutorials is a good idea. I'm learning new things and reinforcing what I already know to the point that I can do some things automatically.

One useful thing I learned today: setting up a Project with TextMate, because I saw a video where a guy was using it and thought "Oh man, why am I still browsing back and forth through a series of folders? That seems MUCH better!" So sometimes it's not a matter of learning new things, it's a matter of learning new ways to use familiar tools.