With my ongoing investigation and fascination with Ruby on Rails I’ve been extremely impressed with the ajax integration. In most development scenarios you’re left with conjuring up the XMLHTTPRequest code on the client. While I’m a huge advocate of actually understanding the lower-level code I’m an even bigger advocate of abstraction. That’s why ColdFusion has always appealed to me. I’m becoming even more interested in ROR since it shields me from writing any JavaScript when using ajax. I’m not saying this is true in all cases but definitely for a majority of them.
So I wanted to walk through a simple example similar to my
ColdFusion/Ajax IP address example. I don’t have ROR currently running on my server so this will walk you through running the example locally. I’m assuming that you have ROR up and running. If not, you can
click here to learn more about installing it.
The first thing we’ll do is create our rails application in the appropriate directory. Within this directory you’ll want to issue the
rails ajax_demo
command to create the application framework. Now that the framework is created let’s go ahead and start the built-in Ruby WEBrick server. You’ll want to cd into the ajax_demo directory and issue the
ruby script/server
command to start the server. We can leave this running while we edit and save files for testing purposes. The application should now be running at http://localhost:3000. Now we’ll go ahead and create a controller so we can ultimately add the “get_ip” action to it. The command to generate the controller is
ruby script/generate controller Test
I use the controller name “Test” because I’m lacking creativity at this moment! Feel free to call this what you like as it won’t matter much in this example. You should now be able to browser to http://localhost:3000/test and receive the default “No action responded to index” message. Let’s fix this. Instead of getting into the details of creating different methods within our controller I’m going to create simple views (.rhtml files) that will be rendered when actions are invoked. Let me elaborate a little more. If you browse to the app/controllers/test_controller.rb file you’ll see the controller skeleton. If you add the following code within the class definition
and access http://localhost:3000/test again you’ll see the text printed to the page. The index method is the default for the controller and is what gets rendered and displayed to the user. If the method doesn’t exist then the views/test/index.rhtml view gets rendered. So let’s remove the method from the controller and create our index.rhtml view. The code looks something like
Notice the inclusion of the JavaScript prototype library which contains all of the necessary ajax functions. The next thing you’ll notice is the link_to_remote method which takes three paramers: 1. the text to display for the link 2. the id of the div to update and 3. the action to call. In our case we’re updating the “ipdiv” and calling the “get_ip” action.
Now we’re going to look into creating the get_ip method in our Test controller. Here’s the controller’s code