Forum: Ruby on Rails Inline RJS in controller

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John (Guest)
on 2006-04-01 17:53
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
Cody F. (Guest)
on 2006-04-02 02:26
(Received via mailing list)
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
John (Guest)
on 2006-04-02 03:27
Hi Cody,

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

Thanx.
This topic is locked and can not be replied to.