# Ajax and Controller actions

**URL:** <https://www.ruby-forum.com/t/ajax-and-controller-actions/99364>\
**Category:** Rails\
**Created:** [January 31, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/ajax-and-controller-actions/99364 "2006-01-31T14:42:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [January 31, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/ajax-and-controller-actions/99364/1 "2006-01-31T14:42:46Z")

</div>

I am new to rails and want to learn the ‘best practices’ up front. So I  
have a few questions and hope someone can help:

1. Using Ajax to update a div. The remote\_function must call an action  
on a controller. This causes me two problems:  
1.a The action is only ever used by the ajax call, but can now be  
accessed through the browser. I want ajax to call a method/action that  
is not accessible to a user.  
1.b The action must replace the entire div. That is fine, but now I  
have duplicate presentation logic. The view renders the div with  
something like ‘select’ helper, and now the action called by ajax has to  
do the same - but I cannot use the ‘select’ helper in the controller  
(not that I can find) so I have to create the and tags  
with more verbose ruby logic.

I know ruby on rails does not like you to repeat yourself so I have no  
doubt I am doing something wrong. How can I have only one piece of  
presentation logic shared by both the controller action, and the view?  
And, how can I use the ‘select’ helper in the action instead of manually  
coding the tags. Or, is there a better approach I am not seeing?

1. Business logic. I am fairly new to MVC. Where does the business  
logic sit? I am sure it is not in the view (when therfore also rules  
out the view helpers in my mind), and it does not seem right in the  
controller since each public method is accessible through the browser.  
So I am left assuming the bulk of the application is implemented in the  
model?. I want to have the appropriate level of separation between  
layers. Can someone tell me if I am on the right track?

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [January 31, 2006, 2:48pm UTC](https://www.ruby-forum.com/t/ajax-and-controller-actions/99364/2 "2006-01-31T14:48:30Z")

</div>

On 1/31/06, Jason B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> have duplicate presentation logic. The view renders the div with  
> something like ‘select’ helper, and now the action called by ajax has to  
> do the same - but I cannot use the ‘select’ helper in the controller  
> (not that I can find) so I have to create the and tags  
> with more verbose ruby logic.
> 
> I know ruby on rails does not like you to repeat yourself so I have no  
> doubt I am doing something wrong. How can I have only one piece of  
> presentation logic shared by both the controller action, and the view?  
> And, how can I use the ‘select’ helper in the action instead of manually  
> coding the tags. Or, is there a better approach I am not seeing?

Use partial view templates.

1. Business logic. I am fairly new to MVC. Where does the business

> logic sit? I am sure it is not in the view (when therfore also rules  
> out the view helpers in my mind), and it does not seem right in the  
> controller since each public method is accessible through the browser.  
> So I am left assuming the bulk of the application is implemented in the  
> model?. I want to have the appropriate level of separation between  
> layers. Can someone tell me if I am on the right track?

I’m by no means an expert, but I usually divide up my business logic  
based  
on what it operates on - if it’s a core part of the model, then it goes  
into  
the model - validation, complex getters / setters, etc. If it involves  
the  
control of the web app in any way (i.e., deciding where to redirect,  
picking  
a partial to display, etc.) it goes in the controller.

Thanks,

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [January 31, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/ajax-and-controller-actions/99364/3 "2006-01-31T14:53:10Z")

</div>

Will B. wrote:

> Use partial view templates.

Thanks Will. I tried using a partial and that works fine for the view,  
but when I use render\_partial in the action called by the ajax - which  
is only supposed to update the div - it tried to overwrite the entire  
page and it all goes pear shaped.

Have you got that to work? Render a partial in response to an ajax  
call?

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [January 31, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/ajax-and-controller-actions/99364/4 "2006-01-31T15:03:42Z")

</div>

Jason,

Try adding  
:layout =\> false  
to your render call.

-bakki

---

<div class="post-metadata">

**Author:** ![jasonb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a587f6/32.png) [@jasonb](https://www.ruby-forum.com/u/jasonb)\
**Post date:** [January 31, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/ajax-and-controller-actions/99364/5 "2006-01-31T21:56:39Z")

</div>

use  
if request.xhr?  
render :layout =\> false  
else  
render :layout =\> true  
end

On 1/31/06, Bakki K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Will B. wrote:
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

Rgds,  
–Siva J.  
[http://www.varcasa.com/](http://www.varcasa.com/)  
My First Rails Project. Powered by Typo and soon by RForum too
