2017-11-09 8 views
0

사용 레일 5.1.4 GET, 루비 2.4루비 - 패러데이에 NilClass 오류가 요청

나는 문제없이 이전에했던 패러데이를 사용하여 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 ("/ 의회/V1/회원/집/# {주} /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/[email protected]/gems/rack-2.0.3/lib/rack/etag.rb:25:in `call' 
# /Users/user/.rvm/gems/[email protected]/gems/rack-2.0.3/lib/rack/conditional_get.rb:25:in `call' 
# /Users/user/.rvm/gems/[email protected]/gems/rack-2.0.3/lib/rack/head.rb:12:in `call' 
# /Users/user/.rvm/gems/[email protected]/gems/rack-2.0.3/lib/rack/session/abstract/id.rb:232:in `context' 
# /Users/user/.rvm/gems/[email protected]/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/[email protected]/gems/rack-2.0.3/lib/rack/method_override.rb:22:in `call' 
# /Users/user/.rvm/gems/[email protected]/gems/rack-2.0.3/lib/rack/runtime.rb:22:in `call' 
# /Users/user/.rvm/gems/[email protected]/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/[email protected]/gems/rack-2.0.3/lib/rack/urlmap.rb:68:in `block in call' 
# /Users/user/.rvm/gems/ruby-2.4.0[email protected]/gems/rack-2.0.3/lib/rack/urlmap.rb:53:in `each' 
# /Users/user/.rvm/gems/[email protected]/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

당신은에서'end' 라인 중 하나를 이동해야 내가 문제를 표시하도록 예를 청소 한 방법의 끝은 두 번째'패러데이 (faraday) '라인 다음에있다. 아마도 당신은 리팩터링했고 잘못된 장소에서 끝났을 것이므로 패러데이 연결을 위해 초기화 프로그램 블록에서 모든 것을하려고합니다. –

답변

1

@conn가 제대로 정의되지 않는, 그것은 시도 그렇게 할 때 @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 
+0

그게 다야 - 고마워! – cborders