使用Rails 5.1.4,Ruby 2.4
我正在尝试使用Faraday连接到一个API,我之前没有遇到过问题,但是当我运行RSpec时出现以下错误:
SearchController GET #index返回http成功 失败/错误:响应= @ conn.get(“/ congress / v1 / members / house /#{state} /current.json”)
NoMethodError:
undefined method `get' for nil:NilClass
Did you mean? gem
# ./app/controllers/search_controller.rb:9:in `block in index'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday/connection.rb:88:in `initialize'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# ./app/controllers/search_controller.rb:5:in `index'
# ./spec/controllers/search_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
2)用户可以搜索住宅成员用户提交有效的州名 失败/错误:响应= @ conn.get(“/ congress / v1 / members / house /#{state} /current.json”)
NoMethodError:
undefined method `get' for nil:NilClass
Did you mean? gem
# ./app/controllers/search_controller.rb:9:in `block in index'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday/connection.rb:88:in `initialize'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/faraday-0.13.1/lib/faraday.rb:67:in `new'
# ./app/controllers/search_controller.rb:5:in `index'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/etag.rb:25:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/conditional_get.rb:25:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/head.rb:12:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:232:in `context'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:226:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:36:in `call_app'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `block in call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/rack/logger.rb:24:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/sendfile.rb:111:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/railties-5.1.4/lib/rails/engine.rb:522:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/urlmap.rb:68:in `block in call'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `each'
# /Users/user/.rvm/gems/ruby-2.4.0@global/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `call'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/mock_session.rb:30:in `request'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/test.rb:249:in `process_request'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/rack-test-0.7.0/lib/rack/test.rb:59:in `get'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:69:in `process'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:41:in `process_and_follow_redirects'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/browser.rb:32:in `submit'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/form.rb:78:in `submit'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/rack_test/node.rb:64:in `click'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/element.rb:143:in `block in click'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/base.rb:85:in `synchronize'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/element.rb:143:in `click'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/node/actions.rb:25:in `click_link_or_button'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/session.rb:776:in `block (2 levels) in <class:Session>'
# /Users/user/.rvm/gems/ruby-2.4.0/gems/capybara-2.15.1/lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
# ./spec/features/user_can_search_by_state_spec.rb:11:in `block (2 levels) in <top (required)>'
这是我的搜索控制器,逻辑所在的位置,直到我可以修复此错误并将其解压缩到别处:
class SearchController < ApplicationController
def index
state = params[:state]
@conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
faraday.headers["X-API-KEY"] = "<$MY-API-KEY>"
faraday.adapter Faraday.default_adapter
response = @conn.get("/congress/v1/members/house/#{state}/current.json")
results = JSON.parse(response.body, symbolize_names: true)[:results]
@members = results.map do |result|
Member.new(result)
end
end
end
end
要彻底,这是我的会员模型:
class Member
attr_reader :name,
:role,
:party,
:district,
:seniority
def initialize(attributes = {})
@name = attributes[:name]
@role = attributes[:role]
@party = attributes[:party]
@district = attributes[:district]
@seniority = attributes[:seniority]
end
end
答案 0 :(得分:1)
@conn.get
未正确定义,因此当它尝试调用nil.get
时,它实际上正在调用undefined method get for nil
,这会引发@conn
。确保在尝试引用之前已定义class SearchController < ApplicationController
def index
state = params[:state]
@conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
faraday.headers["X-API-KEY"] = "<$MY-API-KEY>"
faraday.adapter Faraday.default_adapter
# you are trying to reference @conn here
response = @conn.get("/congress/v1/members/house/#{state}/current.json")
results = JSON.parse(response.body, symbolize_names: true)[:results]
@members = results.map do |result|
Member.new(result)
end
end # but @conn is not defined until you get here
end
end
。我已经清理了您的示例以显示问题:
class SearchController < ApplicationController
def index
state = params[:state]
# fully define your @conn object
@conn = Faraday.new(url: "https://api.propublica.org") do |faraday|
faraday.headers["X-API-KEY"] = "<$MY-API-KEY>"
faraday.adapter Faraday.default_adapter
end
# now you can reference it
response = @conn.get("/congress/v1/members/house/#{state}/current.json")
results = JSON.parse(response.body, symbolize_names: true)[:results]
@members = results.map do |result|
Member.new(result)
end
end
end
如果您重构代码,它应该看起来像这样:
direct