It has been a few weeks now since I started programming in Ruby and Rails. I really have it enjoyed it and I’m very impressed with Rails. However, I recently took some time to learn about the Ajax support in Rails and came away somewhat confused.
You see, Rails is a MVC (mode-view-controller) framework. However, the Ajax support in rails seems to encourage a developer to include view logic in their controller. Since one of the main benefits of Rails is the MVC framework, I was a bit confused by this.
Now, to be fair, my opinions here were formed after reading one article, Ajax on Rails by Curt Hibbs, and I’m still a Rails newbie.
Let me provide an example of what I found concerning. There form_remote_tag contains a paramter where you specify the ID of a DOM element that will be updated the action contain within the controller code.
For example,
form_remote_tag(:update => “my_list”, :url => { :action => :add_item }, :position => “top”)
This code snippet inidcates that add_item action is called in the controller when the form is submitted.
The controller code looks like this,
class ListdemoController < ApplicationController
def add_item
render_text "<li>" params[:newitem] "</li>"
end
end
As you can see, HTML is generated from the controller. This means if you change the code in your view, you may need to go back and change the HTML generated by the controller so that is fits with the changes you made to your view. This is excatly the type of thing Rails avoids in so many other instances, and exactly why I’m so excited about Rails. What’s up with that!
I began to poke around the Ajax for Rails API documentation and found some reference to registering a client side javascript function that would be called. However, I wasn’t able to make this all work. I wasn’t able to find any good examples online and hope the new Rails book I ordered (Agile Web Development with Rails) will help.
In any event, I’m still very excited about Rails and hope this means there is still some opportunity for me to add to the community once I feel like I’m overcome the learning curve.