how to render a rabl view in a rake task?
Asked Answered
E

1

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".

Elgar answered 3/4, 2012 at 19:11 Comment(0)
M
6

There is an easier way to do that which is by calling Rabl::Renderer methods. However your context will matter when you try to render something. Therefore, be careful if there is any controller/helper methods inside your rabl files because they will not be available when calling it outside. Here is a sample code that you can use, tested with Rabl 0.6.13.

Rabl::Renderer.json(@post, 'posts/show', :view_path => 'app/views')
Maiden answered 5/7, 2012 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.