How to serve a static JSON file in rails?

Put it in public/assets/javascripts. Or app/assets/javascripts. The server may even return the correct content-type.


  • put data.json file into your project directory ( for example public/data.json)
  • @data = File.read("#{Rails.root}/public/data.json")
  • At last but not least render :json => @data

I think this is a scope issue; your outer @data is not the same as the @data in a method. That is, you can't use instance variables as expected outside of methods because there is no instance yet.

It should work if you use a class variable, eg

@@data = File.read("/path/to/data.json")

then

render :json => @@data