# Inline RJS in controller

**URL:** <https://www.ruby-forum.com/t/inline-rjs-in-controller/56601>\
**Category:** Rails\
**Created:** [April 1, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/inline-rjs-in-controller/56601 "2006-04-01T15:53:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 1, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/inline-rjs-in-controller/56601/1 "2006-04-01T15:53:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 2, 2006, 12:26am UTC](https://www.ruby-forum.com/t/inline-rjs-in-controller/56601/2 "2006-04-02T00:26:38Z")

</div>

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](mailto:removed_email_address@domain.invalid) wrote:

> Question:  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Cody F.  
[http://www.codyfauser.com](http://www.codyfauser.com)

---

<div class="post-metadata">

**Author:** ![john\_old](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@john\_old](https://www.ruby-forum.com/u/john_old)\
**Post date:** [April 2, 2006, 1:27am UTC](https://www.ruby-forum.com/t/inline-rjs-in-controller/56601/3 "2006-04-02T01:27:18Z")

</div>

Hi Cody,

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

Thanx.
