Render_to_string in a model?

I have a batch job that will call script/runner, I would like in this
call to create html formated email and save it to the database. This is
easy enough in a controller with
yournewhost.email = render_to_string :action => “email_newhost_ready”,
:layout => false

But render_to_string is only available in controller action pack, is
there a easy way to render_to_string in a Model? Or is there a way to
call a controller action from script/runner?

Glenn C. wrote:

But render_to_string is only available in controller action pack, is
there a easy way to render_to_string in a Model? Or is there a way to
call a controller action from script/runner?

Unfortunately yes. Write this:

class BigController…
def action
yo = Model.new
yo.do_mail(self)

class Model
def do_mail(context)
… context.render_to_string :action => …
end

Now consider refactoring that differently, before the MVC Police get on
your
tail. Maybe it should go like this:

class BigController…
def action
yo = Model.new
yo.do_mail() do |whatever|
render_to_string …
end

I haven’t found a way to abuse blocks in Ruby yet, but I’m sure it’s
theoretically possible…


Phlip
Test Driven Ajax (on Rails) [Book]
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax

Phlip wrote:

Glenn C. wrote:

But render_to_string is only available in controller action pack, is
there a easy way to render_to_string in a Model? Or is there a way to
call a controller action from script/runner?

Unfortunately yes. Write this:

class BigController…
def action
yo = Model.new
yo.do_mail(self)

class Model
def do_mail(context)
… context.render_to_string :action => …
end

Now consider refactoring that differently, before the MVC Police get on
your
tail. Maybe it should go like this:

class BigController…
def action
yo = Model.new
yo.do_mail() do |whatever|
render_to_string …
end

I haven’t found a way to abuse blocks in Ruby yet, but I’m sure it’s
theoretically possible…


Phlip
Test Driven Ajax (on Rails) [Book]
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax

Thanks for the posting, this is sort of what I am looking for, however
when I try a simple example as above I get the error
“NoMethodError: protected method `render_to_string’ called for”

Phlip, so I got confused if I fire up script/runner how would I call the
model method correctly to trigger the render_to_string?

A much easier solution is described here:

http://www.omninerd.com/articles/render_to_string_in_Rails_Models_or_Rake_Tasks

Glenn C. wrote:

class BigController…
def action
yo = Model.new
yo.do_mail() do |whatever|
render_to_string …
end

Thanks for the posting, this is sort of what I am looking for, however
when I try a simple example as above I get the error
“NoMethodError: protected method `render_to_string’ called for”

Try passing the outer context into the inner block:

    that = self
    yo.do_mail() do |whatever|
        that.render_to_string ...
    end

That avoids continuation magic.

Phlip, so I got confused if I fire up script/runner how would I call the
model method correctly to trigger the render_to_string?

I only know how to create a Controller context in two situations - a
real
web server, or a mocked unit test.

If all you need is a string, the following hack will do until someone
points
out the third alternative. Write a unit test that does what you need -
as
production code - and then call it on command. You shouldn’t even need
the
scripts; just say ruby my_hacks/my_fake_test.rb

That’s all because test_helper.rb pulls in exactly the right context to
test
with, so it can mock Controllers. So you can use one as a mock-mock, to
support production code!

But, like all hacks, there’s probably a brilliant solution I have
overlooked. You could, for example, generate your HTML with
Builder::XmlMarkup, instead of render_to_string…