I am writing a rails task that needs to call a controller method. Does anyone know how I can do this? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 27 Aug 2008, at 21:18, Cory Patterson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:> > I am writing a rails task that needs to call a controller method. > Does > anyone know how I can do this? >Not easily. You''re better off pushing the code you need into a model or something Fred> Thanks > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I am not quite sure how it would work with rake tasks, but when I want a plain old ruby script to do work with models (never tried with controllers) I load the rails environment using (assuming that the folder the script resides in the script folder) require File.dirname(__FILE__) + ''/../config/environment'' On Wed, Aug 27, 2008 at 4:52 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On 27 Aug 2008, at 21:18, Cory Patterson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > wrote: > > > > > I am writing a rails task that needs to call a controller method. > > Does > > anyone know how I can do this? > > > Not easily. You''re better off pushing the code you need into a model > or something > > Fred > > Thanks > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I was thinking that too since is really an instance method in my application. It worked except for one part, I am using render_to_string in my controller and I could not figure out how to use that method in my model. Is this the job for a helper? Thanks Frederick Cheung wrote:> On 27 Aug 2008, at 21:18, Cory Patterson > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > > wrote: > >> >> I am writing a rails task that needs to call a controller method. >> Does >> anyone know how I can do this? >> > Not easily. You''re better off pushing the code you need into a model > or something > > Fred-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok, I got it working. I moved the method to the model and after some looking around, found out how to render from a model. I used: av = ActionView::Base.new(Rails::Configuration.new.view_path) product_description = av.render(:partial => ''admin/product/prod_template''... I passed some locals to the partial, changed the variables in the view and everything appears to work. Of note: you must give the complete relative path of the template in the render call or you get the cryptic error: "undefined method `controller_path'' for NilClass:Class" instead of the normal "no delegate template" e.g. the template above was located at "app/views/admin/product/_prod_template.rhtml" Hopefully this will help someone else. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---