Jan 19

The controller for this exercise includes two methods. One for displaying the main page, and a second for generating the select box. Let’s start with the page. We’ll call it order_list:

def order_list

end

That was easy. Nothing here. You’ll see why in the view.

Now for the second method to populate the second select box:

def fill_orders_box
results = Order.find_all_by_customer_id(@params[:customer_id])
render :partial => ‘options’
end

There’s a few things here that I wanted to comment on. The first is the find command on the Order model. ActiveRecord does this very slick translation when you use the findallby_ or findby followed by the column to search on. When ActiveRecord sees this, it automagically translates it to the equivalent search string. For instance the above example translates to:


@results = Order.find(:all,
:conditions => ["customer_id = ?", @params[:customer_id])

Now let’s be honest… that’s pretty cool.

The second thing is the use of the partial. In this example I’m using a partial to fill in the select box option tags. This command makes that happen.

Enjoy the View

The view is where I create the form and put in the first select box, the placeholder for the second select box and the AJAX commands to tie them all together. Here’s the code:

<%= javascript_include_tag "prototype" %>
<select id="order[customer_id] name=”order[customer_id]“>
<%= options_from_collection_for_select(
Customer.find_all_by_customername, “id”, “customername”) %>
</select>

<span id=”order_id_list”>
<select id=”order_id” name=”order[id]“></select>
</span>

<%= observe_field(”order[customer_id]“,
:frequency => 0.25,
:update => “order_id_list”,
:url => {:action => :get_orders},
:with => “‘customer_id=’+value”)
%>

So theres a few things to discuss here. At the top is the javascript_include tag. This is telling the view to use the prototype javascript file in the rails package to handle the AJAX calls. Don’t forget this, it’s what you call ‘important’.

The first select box is straight-forward, but it’s filled with another slick rails deal. Optionsfromcollectionforselect takes the first parameter (note the findallby thing again) to get the recordset to fill it with (hence nothing in the controller for this), the id field is next, and lastly the text for the option statement. Very slick indeed!

Next we have the placeholder for the second select box. In this case I used a span, but div will work too. Then I put an empty select box so that it’s there from the start, you can leave this out if you want it to appear when you click a customer in the first box. Also worth mentioning here is something Rory said on his site, you can’t change the innerHTML of a select box in IE, so that’s the reason for the span, we’ll change what’s in there with the partial.

Now for the AJAX. The last part is the listener. This makes it so the page is always looking for the first select box to change. It looks at the frequency of once ever .25 seconds. When it senses a change, it calls the URL with the included with value as a key and updates the orderidlist span. There’s a bunch of magic that happens in the background, but let’s face it, that’s the best part about rails!

Partial Credit

The last part is the partial. This file is called _options.rhtml. Note the underscore…this is what makes a partial a partial. Because DHH said so.

<select id="order_id" name="order[id]“>
<% for order in @results do -%>
<option value=”<%=order.id %>” >
<%= order.order_number %>
</option>
<% end -%>
</select>

That’s it. Here we generate the select box, loop through the records that are sent from the controller and create a slew of option tags.

So there you have it. As I said before, I’m fairly new to rails and ruby, so if you find flaws in my code (which shouldn’t surprise you) please leave a comment and let me know. I hope this helps someone!

Jan 02

Maggie had her first Christmas (not very exciting for her, we opened her presents while she was asleep… hey, she’s 2 months old!) and her first New Years Eve celebration. She’s not much of a party animal though, she was in bed by 8:30, unlike her mom and dad who made it to about 10:30! We are some crazy folks!

So it’s resolution time, and since I’m not much in to “I’m going to loose 20lbs” kind of resolutions (though that’s not a bad idea…), I thought I’d list the things I’d like to accomplish this year. So here goes:

Learn more about Ruby on Rails

I’ve had a chance to learn a bit about RoR in the last few months during the Typo theme contest. I’m really loving it. It’s a great framework based on a very polished language. It beats any php frameworks I’ve used in the past simply because a lot of people are using it and there’s great documentation. This is my biggest goal of the year.

Team|Worship

I’m currently working on an application that can be used by churches to help organize their worship services. As a worship leader, I know that there are times that I knew more about what’s going on, who’s playing, what songs are overused, etc. So I set out to create a tool that churches can use to help out their worship leaders. Note: I’m writing it in Ruby on Rails… see above :)

Song Writing

Over the last year, I have put together a small project studio in my basement, I’ve got a control surface, a very nice Studio Projects C1 mic, and a Shure Beta 87a along with a tube preamp by ART and brand new AMD Athlon64 box running XP and Sonar. If I can find time between being a dad, work and the above two items, I’d love to work on a few projects in there

Daddy time…

I’m in unchartered waters here… at least for me! I’ve always wanted to be a dad, and I knew it would be a challenge, but I want to be the best dad I can be. So this one is simple. Be a good dad.

So I guess that’s it for now. We’ll see how it goes…