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 <select> and <option> 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? 2. 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 -- Posted via http://www.ruby-forum.com/.
On 1/31/06, Jason Bell <jason.b@aanet.com.au> wrote:> > 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 <select> and <option> 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. 2. 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,> Jason > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060131/5107ff03/attachment-0001.html
Will Briggs 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 -- Posted via http://www.ruby-forum.com/.
Jason, Try adding :layout => false to your render call. -bakki On 1/31/06, Jason Bell <jason.b@aanet.com.au> wrote:> > Will Briggs 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060131/69325f7e/attachment.html
use if request.xhr? render :layout => false else render :layout => true end On 1/31/06, Bakki Kudva <bakki.kudva@gmail.com> wrote:> > Jason, > > Try adding > :layout => false > to your render call. > > -bakki > On 1/31/06, Jason Bell <jason.b@aanet.com.au> wrote: > > > > Will Briggs 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 > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Rgds, --Siva Jagadeesan http://www.varcasa.com/ My First Rails Project. Powered by Typo and soon by RForum too -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060131/9cc7817e/attachment-0001.html