title: how can I save the form responses in a JSON file in ruby?
body: <p>I'm trying to store the responses that are submitted on my form/survey to a JSON file. Currently my JSON file returns null. I'm having trouble figuring out how to save responses on my form to a JSON file.</p>
  
  <h1>survey.rb</h1>
  
  <pre><code>require 'json'       
  
  class Survey
    attr_reader :nationality, :employed, :income, :email
  
    def initialize(args={})
      @nationality = args["nationality"]
      @employed = args["employed"]
      @income = args ["income"]
      @email =args ["email"]
    end
  
  
      def save
        json = {
          email: @email,
          nationality: @nationality,
          employed: @employed,
          income: @income
        }.to_json
  
        open('answers.json', 'a') do |file|
          file.puts json
        end
      end
  
  end
  </code></pre>
  
  <h1>app.rb</h1>
  
  <pre><code>require 'rack'
  require_relative 'route'
  require_relative 'template'
  require_relative 'admin'
  require_relative 'survey'
  
  
  class App
  
    def call(env)
      response_headers = {}
  
      request_cookies = Rack::Utils.parse_cookies(env)
      route = Route.new(env)
      status = route.name =~ /^\d\d\d$/ ? route.name.to_i : 200
      template = Template.new(route.name, visit_count: request_cookies["session_count"].to_i+1)
  
      [status, response_headers, [template.render]]
  
    end
  
  
  
    survey = Survey.new ''  
    survey
    survey.save
  
  end
  </code></pre>
  
  <h1>admin.rb</h1>
  
  <pre><code>require 'sinatra/base'
  require 'rack'
  require 'erb'
  require_relative 'route'
  
  class Admin &lt; Sinatra::Base
  
  
  
  
  
  
    use Rack::Auth::Basic, "Protected Area" do |username, password|
      username == 'minhaj' &amp;&amp; password == 'minhaj'
    end
  
    get '/'  do
      "&lt;div&gt;hi for some reason I don't need to put this in a string #{5+5}&lt;/div&gt;
          &lt;p&gt;
          &lt;/div&gt;hello&lt;/div&gt;
          &lt;/p&gt;
  
      "
    end
  
  end
  </code></pre>
  
  <h1>route.rb</h1>
  
  <pre><code>class Route
    ROUTES = {
      "GET" =&gt; {
        "/"  =&gt; :home,
        "/admin"  =&gt; :admin,
        "/authentication" =&gt; :authentication,
      },
      "POST" =&gt; {
        "/check" =&gt; :check
      }
    }
  
    attr_accessor :name
  
    def initialize(env)
      path = env["PATH_INFO"]
      http_method = env["REQUEST_METHOD"]
      @name = (ROUTES[http_method] &amp;&amp; ROUTES[http_method][path]) || :not_found
    end
  end
  </code></pre>
  
  <h1>config.ru</h1>
  
  <pre><code>#! usr/bin/env ruby
  require 'rack'
  load 'admin.rb'
  load 'visit_counter.rb'
  load 'app.rb'
  
  
  
  
  require File.expand_path '../admin.rb', __FILE__
  
  use Rack::Static, :urls =&gt; ['/stylesheet/style.css'], :root =&gt; 'public'
  
  
  run Admin.new
  run App.new
  use VisitCounter
  
  run Rack::URLMap.new({ "/admin" =&gt; Admin.new, "/" =&gt; App.new })
  </code></pre>
  
  <p>This is how it looks so far and as previously mentioned when I run my app, my answers.json returns </p>
  
  <p><code>{"email":null,"nationality":null,"employed":null,"income":null}</code></p>
  
  <p>instead of returning the values submitted on my form page.</p>