TypeError:没有将nil隐式转换为String错误

时间:2014-01-14 21:53:12

标签: ruby

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

我找到了这个tutorial并在rails控制台中测试它。

我的课程是:

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

控制台中的输出如下

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 > 

第38行就是这个

response = JSON.parse(data)

所以我猜我没有回复。我对此比较陌生,不知道如何分解原因。

提前感谢您的帮助。

尼克

1 个答案:

答案 0 :(得分:4)

错误在此行

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

您假设post_form同时返回响应和数据对象,但看起来data的值为零。

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

您应该可以使用resp.body

获取它
response = JSON.parse(resp.body)