[英]calling ruby method in rails controller raises error
我有一個Rails應用程序,試圖從控制器中調用以下方法,但出現此錯誤:
undefined local variable or method "service" for #<EventsController:0x007fb6d27da1c8>` for this line: `api_method: service.freebusy.query
這是什么問題 如果在服務get_busy_events
上面定義了get_busy_events
為什么看不到它呢?
調節器
include GoogleCalendarApi
.....
@user = current_user
@google = @user.socials.where(provider: "google_oauth2").first
unless @google.blank?
@client = init_google_api_calendar_client(@google)
@result = open_gcal_connection(get_busy_events, @client, @google)
LIB / google_api_calendar.rb
def init_google_api_calendar_client(google_account)
#method only called if google_oauth2 social exists
client = Google::APIClient.new
client.authorization.access_token = google_account.token
client.authorization.client_id = ENV['GOOGLE_API_KEY']
client.authorization.client_secret = ENV['GOOGLE_API_SECRET']
client.authorization.refresh_token = google_account.refresh_token
return client
end
def open_gcal_connection(options, initialized_client, social_object)
client = initialized_client
old_token = client.authorization.access_token
service = client.discovered_api('calendar', 'v3')
result = client.execute(options) #after execution you may get new token
# update token if the token that was sent back is expired
new_token = client.authorization.access_token
if old_token != new_token
social_object.update_attribute(token: new_token)
end
return result
end
def get_busy_events
result = open_gcal_connection(
api_method: service.freebusy.query,
body: JSON.dump({ timeMin: '2015-12-24T17:06:02.000Z',
timeMax: '2013-12-31T17:06:02.000Z',
items: social_object.email }),
headers: {'Content-Type' => 'application/json'})
#handling results
end
要回答您的問題(就像我在評論中所做的那樣):
要修復您的方法,您必須在調用它的操作中定義service
變量。
至於您發布的鏈接:如果您查看get_busy_events
方法,則在其中一行,其中service = client.discovered_api('calendar', 'v3')
很好,因為它在方法中。 service
聲明所依賴的client
如此-您必須在使用它們的方法內部聲明它們。
您應該按照本文並按照此處的代碼進行操作,這樣您將具有:
def init_client
client = Google::APIClient.new
# Fill client with all needed data
client.authorization.access_token = @token #token is taken from auth table
client.authorization.client_id = @oauth2_key
client.authorization.client_secret = @oauth2_secret
client.authorization.refresh_token = @refresh_token
return client
end
您可以使用它在所有其他操作中定義client
變量,然后使用service
方法:
def get_busy_times
client = init_client
service = client.discovered_api('calendar', 'v3')
@result = client.execute(
:api_method => service.freebusy.query,
:body_object => { :timeMin => start_time, #example: DateTime.now - 1.month
:timeMax => end_time, #example: DateTime.now + 1.month
:items => items
},
:headers => {'Content-Type' => 'application/json'}})
end
編輯編號2:
由於您有一個控制器,因此在客戶端初始化的地方,我建議將其作為參數傳遞給:
include GoogleCalendarApi
.....
@user = current_user
@google = @user.socials.where(provider: "google_oauth2").first
unless @google.blank?
@client = init_google_api_calendar_client(@google)
@result = open_gcal_connection(get_busy_events(@client), @client, @google)
並更改您的get_busy_events
方法:
def get_busy_events(client)
service = client.discovered_api('calendar', 'v3')
result = open_gcal_connection(
api_method: service.freebusy.query,
body: JSON.dump({ timeMin: '2015-12-24T17:06:02.000Z',
timeMax: '2013-12-31T17:06:02.000Z',
items: social_object.email }),
headers: {'Content-Type' => 'application/json'})
#handling results
end
盡管這對我來說有點奇怪(像這樣嵌套參數),所以您應該考慮重構它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.