6

I'm trying to render a rabl view to string in a rails 3.2 rake task. I'm rendering it to string in order to send some JSON through Pusher from a background task. I've looked at various render_to_string from rake task answers but none of them seem to work. Here is what I have thus far:

controller = PostsController.new
av = ActionView::Base.new(MyApp::Application.config.paths['app/views'].first,{},controller)
@post = post
Pusher["some channel"].trigger('new_post', av.render(:template => 'posts/show.json.rabl'))

With this attempt I get a ActionView::Template::Error exception and the error "undefined method `parameters' for nil:NilClass".

4

1 回答 1

6

有一种更简单的方法是调用Rabl::Renderer 方法。但是,当您尝试渲染某些内容时,您的上下文将很重要。因此,如果您的 rabl 文件中有任何控制器/帮助程序方法,请小心,因为在外部调用它们时它们将不可用。这是您可以使用的示例代码,使用 Rabl 0.6.13 进行了测试。

Rabl::Renderer.json(@post, 'posts/show', :view_path => 'app/views')
于 2012-07-05T05:57:52.407 回答