No commit activity in last 3 years
No release in over 3 years
This gem provides a JavaScript-based JSON viewer for your Rails 4+ application.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 4.2.0
 Project Readme

json-formatter-rails

Want a quick way to present a JSON data structure in your browser? I did so I selected [https://github.com/mohsen1/json-formatter-js]. You can see it in action at [http://azimi.me/json-formatter-js/]. This gem makes it easier for you to use it for your projects.

Versions

Starting with v1.3.0, this gem's version will match the version of json-formatter-js that is in the gem. If there is a fix to a specific version then the 4th digit will indicate this (i.e. 1.3.0.1)

Installation

The json-formatter file will be added to the asset pipeline and available for you to use. If they're not already in app/assets/javascripts/application.js then add this line:

//= require json-formatter

Example use

Suppose you want to quickly display a bunch of "status" information. And furthermore you want to use an Ajax request to get the most recent status.

In your controller make a status method like:

class InformationController < ApplicationController

  def status
    respond_to do |format|
      format.html
      format.json { render json: MyBigJsonStatus.method }
    end
  end
  
end

Add a route to your routes.rb file

  get '/information/status', to: 'information#status'

Create a view file status.html.erb

    <div id="status">
      Status Information:
    </div>
    
    <script>
      $.getJSON( document.location.href, function( data ) {
          formatter = new JSONFormatter(data);
          $('#status').append(formatter.render());
      });
    </script>

In this example opening http://localhost:3000/information/status will produce your HTML file in your browser that will then fetch the status data, as JSON, via an Ajax request. You can then view the json data comfortably in your browser.

Contributing to json-formatter-rails

Send me a pull request or open an issue.

License

json-formatter-rails is released under the MIT License.