May
14
2006
In a Ruby on Rails application you might have several controllers working together. When you generate a controller Rails also creates a layout for that controller. So if you generate three controllers you also have three layout rthml files under you app/views/layouts directory. If all these controllers are working together for the same application it can be assumed that they have the same look and feel which needs to be repeated in each of the layout rhtml files, well it doesn’t have to. One of the guiding principles of Rails is DRY, Don’t Repeat Yourself. It wouldn’t be a good idea to repeat the look and feel of you application in the layout for each controller.
You can use the render function’s layout symbol to specify which template should be used to display the results of a given controller action. Here is an example of that:
class IndexController < AppplicationController
def index
render :layout => 'mylayout'
end
def list
end
end
In this example, the index action will be rendered using the app/views/layout/mylayout.rhtml layout. For the list action the app/views/layout/index.rhtml layout, the default layout for this controller, will be used to render the action’s HTML.
If you want to use a given layout for all of a controller’s actions you can do the following:
class IndexController < AppplicationController
layout 'mylayout'
def index
end
def list
end
end
Technorati Tags: rails, ruby on rails, rhtml
1 comment | posted in Ruby, TechKnow
May
12
2006
Adobe Labs is following the footsteps of Google, and Yahoo and has now released a yet another JavaScript-based Web 2.0 framework. Adobe Labs has recently released the Spry Framework, a client-side Ajax powered JavaScript library. I been playing with Spry and I have to say that I like what I have seen. Unlike other Ajax flavored effects libraries such as Moo.fx, Prototype, and jQuery, the Spry Framework read and transform XML data on the inside an HTML file!
The only concern that I have about Spry at this time is the use of proprietary attributes used to define Spry regions. The use of the Spry attributes are similar to the Struts Logic tag library. You can loop over each row in a tabular data representing a companies employees with the following Spry snippet:
<UL spryregion="dataSetEmployees">
<LI spryrepeat="dataSetEmployees">{@fName} {@lName}</LI>
</UL>
With Spry I could do in the client what I had been doing for years in the server with a custom Java applications and a bunch of XSLT files used to transform XML data into HTML.
If you want to learn more head out to the Spry Framework website. Adobe Labs has plenty of great examples and a pretty detailed tutorial in the Spry website. One last note, Spry makes use of Google’s AJAXSLT. AJAXSLT is a JavaScript implementation of XSL-T.
Technorati Tags: spry, ajaxslt, ajax, framework, javascript, java, xsl, xslt
no comments | posted in HTML/XML, JavaScript, TechKnow
May
11
2006
If you have played with Ruby on Rails you may already be familiar with the ActiveRecord’s find method. Here is a recap, say I have a Item model:
class Item < ActiveRecord::Base
end
In your controller you can use the Item class to find all items that meet a given criteria. Here are some examples:
# Find the lowest priced item
lowest = Item.find(:first, :order => 'price asc')
# Find all items by category
items = Item.find(:all, :conditions => ["category = ?", params[:category])
What I couldn’t fine anywhere is, how to use the ActiveRecord’s find method to find items whose name is like a given parameter. In SQL I could do something like this:
select * from items where name like 'ItemName%';
My first attempt at solving this left me open to hacks via SQL injection. After some thought it occurred to me that I could do the following:
like = params[:name].concat("%")
items = Item.find(:all, :conditions => ["name like ?", like])
I still haven’t found the official way to accomplish the above but in the mean time this gets the job done.
Technorati Tags: ruby, sql, rails, ruby on rails, sql injection
4 comments | posted in Ruby, SQL, TechKnow
May
8
2006
In Java, given an object, an instance of a class, you can get the class name by coding the following:
Class clazz = obj.getClass();
String clazzName = clazz.getName();
Sometimes you want you want to create a Class object for a given class. In this case you can do so by writing code similar to the following example:
Class clazz = MyClass.class;
Many plugin frameworks, including the JDBC Driver Manager will create a Class object without having the knowledge of what class name at compile time. There might be a case where you know a class implements a given interface but you don’t know the class name of the implementation until at runtime when you read it from a properties file. In situations like this you can do the following:
String clazzName = "com.juixe.techknow.MyClass";
...
Class clazz = Class.forName(clazzName);
Sometimes you will need a Class object for a primitive type. You might need a Class object for an int or boolean when dealing with reflection. In this case you do so using the dot class notation on a primitive type. Here is a more elaborate example where we create a Class object for an int primitive:
int newValue = ...
Class clazz = obj.getClass();
Method meth = clazz.getMethod("setValue", new Class[]{int.class});
meth.invoke(obj, new Object[]{new Integer(newValue)});
You can also get the class for an array. The only place where I have ever need the class of an array is when working with reflection. Here is how you can get the class for an array:
Class clazz = String[].class;
Technorati Tags: java, class, object, reflection
no comments | posted in Java, TechKnow
May
6
2006
Recently I started working closely with new developer to our company. I feel he over thinks problems and under estimates solutions. My personal philosophy is that less code is the best code. I guess this is the same thing that Ward Cunningham meant when he said to do the simplest thing that could possibly work. All this is another way of saying Keep It Simple, Stupid. Code should always strive to be simple.
Technorati Tags: programming, kiss principle
no comments | posted in Programming, Rant, TechKnow
May
4
2006
Last week I went up to the Computer History Museum to listen to Ward Cunningham, the inventor of the Wiki Wiki Web. From what I heard, it seems like Ward was also heavily involved with Design Patterns, Extreme Programming, and CRC (Class, Responsibilities, and Collaborations) cards. Ward is a veteran computer scientist, he said that it is “a little scary when you walk in a history museum and you say, ‘I programmed in that, and that, and that.'” Speaking about those first computers that he programed, he said that was that era where ever new machine was a new idea.
What I got from his talk, and what I have been seeing in the web, is that great code comes from communities. Great examples of this is Apache, Eclipse, Rails, and Wiki itself. Ward also suggested to surround ourselves in a community of learners.
Technorati Tags: wiki, crc, xp, extreme programming, design patterns
no comments | posted in Programming, Rant, TechKnow