I am writing a rails task that needs to call a controller method. Does anyone know how I can do this?
Thanks
I am writing a rails task that needs to call a controller method. Does anyone know how I can do this?
Thanks
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
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’
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:
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.