The blog of Rahoul Baruah from 3hv Ltd

What's going on?

My name is Rahoul Baruah (aka Baz) and I'm a software developer in Leeds (England).

This is a log of things I've discovered while writing software in Ruby on Rails. In other words, geek stuff.

However, I've decided to put this blog on ice - I would ask you to check out my business blog here (or subscribe here).

22 May, 2007

RJS Templates

I love RJS. It means I don't have to wade through Javascript (yeuch). But RJS templates have been bothering me for a while. You see your .rhtml and your .mab files literally are views. They define HTML as it appears on-screen. But your .rjs files are not. They manipulate what is there but they do not define it. The proof of this is when you find yourself writing:


page.replace_html 'something', :partial => 'something', :object => @something


The RJS defines what you want to happen to the page but what actually gets displayed is in _something.mab (or _something.rhtml).

So I have decided to do the following from now on: no more .rjs "views" - instead I will code my controllers like this:


def new
@something = Something.new
respond_to do | format |
format.js { render_new_as_js }
end
end

private

def render_new_as_js
render :update do | page |
page.replace_html 'something', :partial => 'something', :object => @something
page.visual_effect :highlight, 'something'
end
end


In other words code stays in the controller and html stays in the views.

No comments:

eXTReMe Tracker