Ajax and Controller actions

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

On 1/31/06, Jason B. [email protected] 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,

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

Jason,

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

-bakki

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

On 1/31/06, Bakki K. [email protected] wrote:

Will B. wrote:


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

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