简体   繁体   English

在Rails控制器中调用ruby方法会引发错误

[英]calling ruby method in rails controller raises error

I have a rails app and I'm trying to call the methods below from the controller, but I get this error: 我有一个Rails应用程序,试图从控制器中调用以下方法,但出现此错误:

undefined local variable or method "service" for #<EventsController:0x007fb6d27da1c8>` for this line: `api_method: service.freebusy.query

What's the problem here? 这是什么问题 Why can't the get_busy_events see the service var if it's defined above it? 如果在服务get_busy_events上面定义了get_busy_events为什么看不到它呢?

controller 调节器

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 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

To answer your question(as I did in the comments): 要回答您的问题(就像我在评论中所做的那样):

To fix your method, you have to define the service variable in the action where you are calling it. 要修复您的方法,您必须在调用它的操作中定义service变量。

As for your posted link: if you look at the get_busy_events method there is a line where service = client.discovered_api('calendar', 'v3') 至于您发布的链接:如果您查看get_busy_events方法,则在其中一行,其中service = client.discovered_api('calendar', 'v3')

and it is fine, because it is in the method. 很好,因为它在方法中。 The same goes for client that the service declaration depends on- you have to declare them inside the method where you use them. service声明所依赖的client如此-您必须在使用它们的方法内部声明它们。

You should follow the article and make the code as it is there so you would have: 您应该按照本文并按照此处的代码进行操作,这样您将具有:

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

which you can use to define client variable in all your other actions and then use the service method: 您可以使用它在所有其他操作中定义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

EDIT No2: 编辑编号2:

Since you have a controller, where client is initialized I suggest passing it down as an argument: 由于您有一个控制器,因此在客户端初始化的地方,我建议将其作为参数传递给:

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)

and changing your get_busy_events method: 并更改您的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

Although this is a bit weird for me(nesting arguments like this) so you should look at refactoring this. 尽管这对我来说有点奇怪(像这样嵌套参数),所以您应该考虑重构它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM