나는 stackoverflow를 통해 여러 시간을 보냈고보기 파일에서 twitter gem의 메소드를 호출하는 방법에 대한 간단한 설명을 찾을 수 없습니다. 다음 파일을 가지고 매우 간단한 레일을 설정하고이를 테스트하기 위해 트위터 API 클라이언트를 전역 변수로 설정했습니다. 그것은 레일 콘솔에서 작동하지만 레일에서 작동시키지 못합니다. 내가 오류가 undefined method "followers" for nil:NilClass
보기 파일의 twitter api 클라이언트에서 Twitter gem 메소드를 호출하는 방법
설정/초기화/twitter.rb
require 'twitter'
$client = Twitter::REST::Client.new do |config|
config.consumer_key = 'xxxx'
config.consumer_secret = 'xxxx'
config.access_token = 'xxxx'
config.access_token_secret = 'xxxx'
end
Rails.application.routes.draw do
get 'welcome/index'
root 'welcome#index'
end
routes.rb
class WelcomeController < ApplicationController
def index
end
end
welcome_controller.rb index.html.erb (이다 환영합니다 .rb)
<h1>Hi there</h1>
<p><%= $client.user %></p>
구성 파일에 직접 모든 인증 키를 입력 했으므로 사용자 정보를 반환해야합니다.
많은 감사
내 대답은 이것에 어떤 도움이 되었습니까? –