How to pass an object to rabl views from rake task

I am trying to create a json file from a rake task using rabl. Below I have a simplified version for testing.

When I look through 'articles.json' or 'articles / 2.json' via url, I get the expected json response.

But when I try to run it with the rake command, it is always null for @articles in the generated jsonFile. It will show index.json.rabl as many times as @ articles.count, but the values ​​are always zero.

So, how do I pass the @articles object created in my rake command to Rabl.render?

index.json.rabl

@feedName ||= 'default'
node(:rss) { partial('articles/rss), :object => @feedName }
node(:headlines) { partial('articles/show'), :object => @articles }

show.json.rabl

object @article
attributes :id,:body
....

exports.rake

task :breakingnews => :config do
  filename = 'breakingnews.json'
  jsonFile = File.new(filename)
  @articles = Article.limit(10)
  n = Rabl::renderer.json(@articles,'articles/index',view_paths => 'app/views')
  jsonFile.puts b
  jsonFile.close
+2
source share
1 answer

. 2 :

@your_object = ...
Rabl.render(@your_object, 'your_template', view_paths => 'relative/path/from/project/root', :format => :json)

Rabl

object @any_name_you_like
attributes :id,:body
....

json , ( )

. , , - (. ).

@one_object = ...
@another_object = ...
Rabl.render(nil, 'your_template', view_paths => 'relative/path/from/project/root', :format => :json, :scope => self)

Rabl

object @one_object
attributes :id,:body
node(:my_custom_node) { |m| @another_object.important_stuff }
+11

All Articles