首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误TypeError:在rails中使用Twilio响应无法将true转换为字符串

错误TypeError:在rails中使用Twilio响应无法将true转换为字符串
EN

Stack Overflow用户
提问于 2017-03-06 02:11:33
回答 1查看 96关注 0票数 0

我心里有个错误,根本不知道如何解决这个问题。我没有用大量的代码来解决这个问题,而是在问题之后添加了代码。

我正在使用Twilio生成自动电话信息,但是,我不知道问题是否与Twilio宝石有关。从我所做的研究来看,这常常与宝石不相容有关。请参阅下面安装了版本的 see文件。您还可以看到发生错误的错误消息webrick文件。最后,以下是破解的代码

代码语言:javascript
复制
def response
  Rails.logger.debug '+ response' if PHONE_DEBUG
  Rails.logger.debug '* @question.text = ' + @question.text if PHONE_DEBUG

  if (@question.text =~ /hash|\#/i) != nil
    finish_on_key = '#'
  else
    finish_on_key = ''
  end

  Twilio::TwiML::Response.new do |r|
    r.Say @question.text, voice: 'alice', language: 'en-AU'
    r.Gather finishOnKey: finish_on_key, timeout: PHONE_WAIT_TIME,
      action: NGROK + answers_path(@question.id) # Answer create
  end.to_xml
  Rails.logger.debug '- response' if PHONE_DEBUG
end

如果您查看一下错误消息文件,就可以在它爆炸之前从上面的代码中看到logger.debug消息。当我的rails应用程序打电话给我时,上面的代码会在我接电话时运行。它应该问我一个问题(但它从来没有),然后在我回答之后,它会重定向到答案的路径。

韦布里克在这里失败了

代码语言:javascript
复制
body.each { |part|
  res.body << part
}

我对违规的变量运行logger.debug,发现res.body是字符串,part是布尔值,所以消息是有意义的。有人知道如何解决这个问题吗?

Gemfile

代码语言:javascript
复制
*source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.8'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin]

# Coffee script 1.9.0 doesn''t play well with Windows.  SMH
gem 'coffee-script-source', '1.8.0'

# Tabbed Navigation goodies
gem 'tabulous'

# SMS and phone call support
gem 'twilio-ruby'

# Validate phone numbers easily
gem 'phony_rails'

# Secure login
gem 'devise'

group :development, :teachinator, :careinator do
  gem 'rails-erd'     # ERD - Entity-Relationship Diagrams for documentation
  gem 'annotator'     # Annotator - Adds comments to Model files to from DB info
  gem 'faker', '1.7.2'# Helps generate db:seed data
end*

版本

代码语言:javascript
复制
*d:\svn_branch\rails\jobinator>bundle install
Using rake 11.3.0
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.9.1
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using rack 1.5.5
Using mime-types 2.6.1
Using arel 5.0.1.20140414130214
Using bcrypt 3.1.11
Using choice 0.2.0
Using coffee-script-source 1.8.0
Using execjs 2.5.2
Using thor 0.19.1
Using colored 1.2
Using currencies 0.4.2
Using orm_adapter 0.5.0
Using hike 1.2.3
Using multi_json 1.11.2
Using jwt 1.5.6
Using phony 2.2.14
Using bundler 1.13.6
Using tilt 1.4.1
Using ruby-graphviz 1.2.2
Using sass 3.2.19
Using sqlite3 1.3.10
Using faker 1.7.2
Using rdoc 4.2.0
Using tzinfo 1.2.2
Using rack-test 0.6.3
Using warden 1.2.6
Using mail 2.6.3
Using coffee-script 2.4.1
Using uglifier 2.7.1
Using countries 0.9.3
Using twilio-ruby 4.13.0
Using sprockets 2.12.4
Using sdoc 0.4.1
Using activesupport 4.1.8
Using tzinfo-data 1.2015.6
Using actionview 4.1.8
Using activemodel 4.1.8
Using jbuilder 2.3.1
Using phony_rails 0.6.1
Using actionpack 4.1.8
Using activerecord 4.1.8
Using actionmailer 4.1.8
Using railties 4.1.8
Using sprockets-rails 2.3.2
Using annotator 0.0.10
Using rails-erd 1.5.0
Using coffee-rails 4.0.1
Using responders 1.1.2
Using jquery-rails 3.1.3
Using rails 4.1.8
Using sass-rails 4.0.5
Using turbolinks 2.5.3
Using devise 3.5.10
Using tabulous 2.1.3
Bundle complete! 18 Gemfile dependencies, 60 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

d:\svn_branch\rails\jobinator>*

错误消息

代码语言:javascript
复制
******** CreateResponse.for
+ response
* @question.text = Please press any key to continue.
- response
Completed 200 OK in 143ms (Views: 0.0ms | ActiveRecord: 105.0ms)
[2017-03-06 11:52:37] ERROR TypeError: can't convert true into String
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/handler/webrick.rb:73:in `block in service'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/body_proxy.rb:31:in `each'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rack-1.5.5/lib/rack/handler/webrick.rb:72:in `service'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
        C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

韦布里克

代码语言:javascript
复制
*require 'webrick'
require 'stringio'
require 'rack/content_length'

module Rack
  module Handler
    class WEBrick < ::WEBrick::HTTPServlet::AbstractServlet
      def self.run(app, options={})
        options[:BindAddress] = options.delete(:Host) if options[:Host]
        options[:Port] ||= 8080
        @server = ::WEBrick::HTTPServer.new(options)
        @server.mount "/", Rack::Handler::WEBrick, app
        yield @server  if block_given?
        @server.start
      end

      def self.valid_options
        {
          "Host=HOST" => "Hostname to listen on (default: localhost)",
          "Port=PORT" => "Port to listen on (default: 8080)",
        }
      end

      def self.shutdown
        @server.shutdown
        @server = nil
      end

      def initialize(server, app)
        super server
        @app = app
      end

      def service(req, res)
        env = req.meta_vars
        env.delete_if { |k, v| v.nil? }

        rack_input = StringIO.new(req.body.to_s)
        rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)

        env.update({"rack.version" => Rack::VERSION,
                     "rack.input" => rack_input,
                     "rack.errors" => $stderr,

                     "rack.multithread" => true,
                     "rack.multiprocess" => false,
                     "rack.run_once" => false,

                     "rack.url_scheme" => ["yes", "on", "1"].include?(ENV["HTTPS"]) ? "https" : "http"
                   })

        env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
        env["QUERY_STRING"] ||= ""
        unless env["PATH_INFO"] == ""
          path, n = req.request_uri.path, env["SCRIPT_NAME"].length
          env["PATH_INFO"] = path[n, path.length-n]
        end
        env["REQUEST_PATH"] ||= [env["SCRIPT_NAME"], env["PATH_INFO"]].join

        status, headers, body = @app.call(env)
        begin
          res.status = status.to_i
          headers.each { |k, vs|
            if k.downcase == "set-cookie"
              res.cookies.concat vs.split("\n")
            else
              # Since WEBrick won't accept repeated headers,
              # merge the values per RFC 1945 section 4.2.
              res[k] = vs.split("\n").join(", ")
            end
          }
          body.each { |part|
            res.body << part
          }
        ensure
          body.close  if body.respond_to? :close
        end
      end
    end
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-06 10:33:04

通过检查错误行机架上的源代码,它似乎试图将响应附加到正文字符串。在您的response代码中,最后一句是布尔方法:

代码语言:javascript
复制
def response
  # ...

  Twilio::TwiML::Response.new do |r|
    # ...
  end.to_xml
  Rails.logger.debug '- response' if PHONE_DEBUG
end

不是发送Twilio,而是返回Rails.logger.debug结果,即true

将其更改为如下所示:

代码语言:javascript
复制
def response
  Rails.logger.debug '+ response' if PHONE_DEBUG
  Rails.logger.debug '* @question.text = ' + @question.text if PHONE_DEBUG

  if (@question.text =~ /hash|\#/i) != nil
    finish_on_key = '#'
  else
    finish_on_key = ''
  end

  # -- Store it in a variable --
  xml = Twilio::TwiML::Response.new do |r|
    r.Say @question.text, voice: 'alice', language: 'en-AU'
    r.Gather finishOnKey: finish_on_key, timeout: PHONE_WAIT_TIME,
      action: NGROK + answers_path(@question.id) # Answer create
  end.to_xml
  Rails.logger.debug '- response' if PHONE_DEBUG

  # -- Return the stored xml --
  xml
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42616479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档