Inline RJS in controller


#1

Inline RJS and Controller

Hi guys,

I really like the idea about inline RJS.
I have about 20 rjs files for this 1 controller.
They are small. Just updating and hiding some div’s.
So to move this into my controller would clean up a lot.
It makes sense because you see in the controller what you do.

Question:

  1. Any penalties for doing this?
  • size of controller increases
  • performance issues
  • moving view actions into controller

Thanks in advance and happy coding.

John


#2

John,

You can keep the inline RJS in the controller clean by creating RJS
helper methods, which allow you to aggregate common functionality
together in one method call.

For example:

def create
@product = Product.create(params[:product])
render :update do |page|
page.insert_html :bottom, ‘products’, :partial => ‘product’
page.visual_effect :highlight, “product-#{@product.id}”
end
end

You could move this to a helper method in the helper module for the
controller:

def insert_product(product)
page.insert_html :bottom, ‘products’, :partial => ‘product’, :object
=> product
page.visual_effect :highlight, “product-#{product.id}”
end

And you end up with:

def create
product = Product.create(params[:product])
render(:update){|page| page.insert_product(product)}
end

Refactoring out all common functionality like this should allow you
have many small, one-line, inline RJS calls.

I don’t see why you’d run into any performance problems, but I’d be
weary of your helper and inline RJS code getting too complex. You can
always move the code back to its own RJS template if your controller
code starts getting too bloated.

On 4/1/06, John removed_email_address@domain.invalid wrote:

Question:
Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Cody F.
http://www.codyfauser.com


#3

Hi Cody,

I see what you mean.
I was also thinking about avoiding duplicate code.
And you showed me how.

Thanx.