Smoke Signals

Friday, February 25, 2005

Ruby Syntax (things to remember)

One that had me looking sideways like a puppy was what is called a symbol. Symbols are use quite a bit in rails and me being the whole-view-of-the-picture-or-I-just-don't-get-it kind of guy I had to understand what the hell they were.

Here is an example of a symbol... :symbolName, or constantVar :symbolName. Here is how Introduction to Ruby for MacOSX describes them: Symbols are used to represent method and instance variable names and can also act as unique values for constants.

So here's where they are weird(to me) They can be used as an enum, an immutable string, also used in accessors(getter and setter) which I think it implemented by the object class eg. attr_accessor :counter. Representing methods eg. fp = m.method( :myMethod ) to use fp.call().

Another idiom that I found strange was the whole methoName? which essentially means that the method is going to return a boolean. I am used to strongly typed C# and dynamic python where you explicitly return true or 1 in python.

Monday, February 21, 2005

Catching errors in rails

For those times when users don't fill out their info correct. Here is how you can handle that in rails... First off read: Active Record API doc That will give you a good idea of what you can catch.

Then to implement: In your model class add a validation check - validates_presence_of :description, :message => "is a required field!"

In your controller class add a class instance var of what you are trying to save with the model. For instance in the todo tutorial you have

def add_item

item = Todo.new # Create a new instance of Todo, so create a new item

@todo = item


The @todo will be used in your html to render the error. Now in your add_item.rhtml template(named after the method) you put either 2 things. <% @todo.errors.each_full do |message| %>

<%= message %>

<% end %>

This will iterate over every error and output its message. Doing this: <%= error_messages_for "todo" %> will give you a bit more but you have less control over the formatting

Running with Rails

I have just started messing with Rails and I like it a lot. I come from the following langs and platforms in order 1st - Usertalk, then to Python and Zope, then to C# in .net which was/is my favorite. However at my current job I can not program in C# for various reasons. Most of what we do here is in PHP and I do not like PHP. Sorry phpeeps, I don't. So I came accross rails and it rocks. So I am going to post my findings here.

Tuesday, February 01, 2005

Boring boring

uh bored