ruby 类型错误:没有将 nil 隐式转换为字符串错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21124788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:21:51  来源:igfitidea点击:

TypeError: no implicit conversion of nil into String error

ruby

提问by nktokyo

I am trying to set up our CRM system to create posts in wordpress when a specific type of record (a listing) is added.

我正在尝试设置我们的 CRM 系统以在添加特定类型的记录(列表)时在 wordpress 中创建帖子。

I found this tutorialand am testing it in the rails console.

我找到了本教程,并在 rails 控制台中对其进行了测试。

My class is:

我的班级是:

require 'rubygems'
require 'open-uri'
require 'json'
require 'net/http'

# Please note that the vanilla wp-json-api plugin does not support user authentication for create_post.
# Check out my fork for authentication support: https://github.com/Achillefs/wp-json-api
class Listing
  API_URI = 'http://my_wp_url/api/'
  API_USER = 'my_username'
  API_PASS = 'my_password'

  attr_accessor :title, :address, :sell_price, :type

  def initialize(opts = {})
    # set all values if valid
    opts.each do |key,val|
      begin
        self.send(:"#{key}=",val)
      rescue NoMethodError => e
        raise ArgumentError("#{key} is not a valid listing attribute")
      end
    end
    self.type = 'post' if self.type == nil
  end

  def publish!
    # Need to get a nonce token from WP in order to create a post
    nonce_response = JSON.parse(open(API_URI + "get_nonce/?controller=posts&method=create_post").read)
    if nonce_response['status'] == 'ok'
      nonce = nonce_response['nonce']
      url = URI.parse(API_URI + "posts/create_post")
      args = { 
        'nonce' => nonce, 'author' => API_USER, 'user_password' => API_PASS, 
        'status' => 'draft', 'title' => self.title
      }
      resp, data = Net::HTTP.post_form(url, args)
      response = JSON.parse(data)
      if response['status'] == 'ok'
        puts response.inspect
      else
        raise response['error'].to_s
      end
    else
      raise nonce_response['error'].to_s
    end

  end
end

And the output in the console is as follows

并且控制台中的输出如下

2.0.0-p353 :001 > require 'listing'
 => true 
2.0.0-p353 :002 > l = Listing.new(:title => "test")
 => #<Listing:0x007fab9286a2c8 @title="test", @type="post"> 
2.0.0-p353 :003 > l.publish!
TypeError: no implicit conversion of nil into String
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/json-1.8.1/lib/json/common.rb:155:in `initialize'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/json-1.8.1/lib/json/common.rb:155:in `new'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/json-1.8.1/lib/json/common.rb:155:in `parse'
    from /Users/Nick/rails_projects/wp_api/lib/listing.rb:38:in `publish!'
    from (irb):3
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
    from /Users/Nick/.rvm/gems/ruby-2.0.0-p353@railstutorial_rails_4_0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
2.0.0-p353 :004 > 

Line 38 is this one

第38行是这个

response = JSON.parse(data)

So I guess I'm getting no response back. I'm relatively new to this and am not sure how to break down the cause.

所以我想我没有得到任何回应。我对此比较陌生,不知道如何分解原因。

Thanks in advance for any help.

在此先感谢您的帮助。

Nick

缺口

采纳答案by Simone Carletti

The error is at this line

错误在这一行

resp, data = Net::HTTP.post_form(url, args)
response = JSON.parse(data)

You are assuming that post_formreturns both a response and a data object, but it looks like the value of datais nil.

您假设它同时post_form返回响应和数据对象,但它的值似乎data为零。

Inspect the content of respand data. I'm quite sure the response body is contained in the respobject.

检查的内容respdata。我很确定响应主体包含在resp对象中。

You should be able to fetch it using resp.body

你应该能够使用它来获取它 resp.body

response = JSON.parse(resp.body)