Nitobi Grid on Rails - Part 1

Even though we're doing a lot of work in Ruby on Rails, and we blog about it a lot, we don't sell a back-end for it. So the question that people may ask is how do we use our Components with Rails?

Here's some information on how I'm using Rails with the current version of Grid. In Rails, you can use Builder to create XML, so the first thing that I am going to create is a grid controller. In that grid controller, I define a getHandler method, which looks like this:

def getHandler
if params[:SortColumn].nil?
@tblcustomer_pages, @tblcustomers = paginate :tblcustomers, :per_page => params[:PageSize].to_i
@tblcustomer_pages, @tblcustomers = paginate :tblcustomers, :per_page => params[:PageSize].to_i , :order => params[:SortColumn] + " " + params[:SortDirection]
render :layout => false

What this does is use the paginate function to do some pagination of the table. We set the PageSize using the Nitobi Grid's PageSize paramater, and we use the SortColumn and the SortDirection coming into the function from the grid to determine the column sorting.

I then have a view (rxml) that looks like this:

xml.instruct! :xml, :version => "1.0"
xml.root(:fields => "CustomerName|ContactName", :keys => "CustomerName|ContactName") {
for tblcustomer in @tblcustomers
xml.e(:a => tblcustomer.CustomerName,:b =>tblcustomer.ContactName, :xk => tblcustomer.CustomerID)

This is very basic. I'm just starting out with builder, so I don't have the Error Handler here. I suppose that I should have that there, but I don't know what will happen if its nil. (I suspect it will break). That's definitely something I'm going to have to ask around about.

And, I make sure that I have the getHandler set in the Grid Definition like this:


Of course, I make sure to include grid and toolkit. Once this is all done, I have a grid that can load. I still have to get the savehandler working on Rails, but this should be enough to get started with Rails. If I find any interesting interactions with Grid, Toolkit and Prototype, I'll post them here.