【问题标题】:how can I save the form responses in a JSON file in ruby?如何将表单响应保存在 ruby​​ 的 JSON 文件中?
【发布时间】:2018-10-23 14:19:25
【问题描述】:

我正在尝试将在我的表单/调查中提交的回复存储到 JSON 文件中。目前我的 JSON 文件返回 null。我无法弄清楚如何将表单上的响应保存到 JSON 文件中。

survey.rb

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

app.rb

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

admin.rb

require 'sinatra/base'
require 'rack'
require 'erb'
require_relative 'route'

class Admin < Sinatra::Base






  use Rack::Auth::Basic, "Protected Area" do |username, password|
    username == 'minhaj' && password == 'minhaj'
  end

  get '/'  do
    "<div>hi for some reason I don't need to put this in a string #{5+5}</div>
        <p>
        </div>hello</div>
        </p>

    "
  end

end

route.rb

class Route
  ROUTES = {
    "GET" => {
      "/"  => :home,
      "/admin"  => :admin,
      "/authentication" => :authentication,
    },
    "POST" => {
      "/check" => :check
    }
  }

  attr_accessor :name

  def initialize(env)
    path = env["PATH_INFO"]
    http_method = env["REQUEST_METHOD"]
    @name = (ROUTES[http_method] && ROUTES[http_method][path]) || :not_found
  end
end

config.ru

#! 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 => ['/stylesheet/style.css'], :root => 'public'


run Admin.new
run App.new
use VisitCounter

run Rack::URLMap.new({ "/admin" => Admin.new, "/" => App.new })

这是到目前为止的样子,正如前面提到的,当我运行我的应用程序时,我的 answers.json 返回

{"email":null,"nationality":null,"employed":null,"income":null}

而不是返回在我的表单页面上提交的值。

【问题讨论】:

  • 您好,欢迎来到 StackOverflow!请编辑您的问题并说明两件事:检查代码缩进并消除任何妨碍阅读和理解代码的多余空格/新行;并提供有关您遇到的任何错误消息、行号等的指示。这里的用户通常更容易不必运行代码来了解确切的问题。
  • 嗨,奥利弗,感谢您的提示。我已经摆脱了不必要的空白。至于错误消息,似乎没有,我的 answers.json 文件只返回 {"email":null,"nationality":null,"employed":null,"income":null} 而不是返回响应填写完毕。
  • @testprogrammer “json 文件返回 null”是什么意思?
  • @Rashmirathi 我已经更新了我的问题。
  • @testprogrammer 你能发布admin.rb和route.rb template.rb的内容吗?另外我相信包含 App 类的文件称为 config.ru 而不是 app.rb。

标签: json ruby rack


【解决方案1】:

也许您可以尝试以下方法:

file = File.open(File.dirname(__FILE__) + '/answers.json', 'a+')
file.write json
file.write "\n"
file.close

首先确保json 确实已被填充,您可以通过puts json 进行检查,或者如果您使用的是rubymine,则可以在调试模式下运行并在其上添加断点。

此外,您不能只在类中放置代码并期望它被执行。

移动:

survey = Survey.new ''  
survey.save

放入一个方法中并具体调用,或者包含在调用方法中。

让我知道它是否有效

【讨论】:

  • 您好,布拉德,感谢您的回答。我在 call 方法中移动了survey = Survey.new ''survey.save。我也试过你的解决方案,我的 answer.json 文件仍然返回 {"email":null,"nationality":null,"employed":null,"income":null}
  • 好的,我认为问题是根本没有任何东西进入您的 json 文件。好吧,您正在调用 args['something'] 但您从未为 args 设置任何值,所以它当然会为空。 When you call Survey.new '' 您将 '' 作为参数传递。我不熟悉直接使用 rack 所以不知道是否应该发生其他一些魔法,但看起来你需要在调用新的调查时传入 args,例如survey new {nationality:'some nationality', etc}
猜你喜欢
  • 2018-12-07
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 2015-09-04
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
相关资源
最近更新 更多