How do I convert a Ruby on Rails model to a .json file?

If I have a basic ruby ​​on a rails model set up (like a typical blogpost app) with an action index

like:

def index
  @blogs=Blog.all
end

      

How do I convert all blogs to a file .json

? I know if I do this:

def index
  @blogs=Blog.all
  respond_to do |format| 
  format.html{}
  format.json{render json: @blogs.to_json}
end

      

This will give json output (which is what I need), but on the following path: /blogs.json in my browser, but I need the actual .json file (not just one of them visible in the browser along the path).

How should I do it?

+3


source to share


1 answer


Just replace

 format.json{render json: @blogs.to_json}

      



format.json{send_data @blogs.to_json}

      

0


source







All Articles