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:
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?
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?
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.
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. 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?