在方法之间共享变量

时间:2016-07-31 23:30:22

标签: ruby-on-rails ruby google-drive-api

我是Ruby / Rails的新手,我很难在两个方法之间传递变量。在这种情况下,我正在使用授权方法和在Google云端硬盘上创建新文件夹的新方法。当我在一个方法中将它们组合在一起时它工作正常但是当我将它们分开时,我收到了这个错误" EssaysController中的NoMethodError #new undefined方法`create_file'为零:NilClass"。我尝试将驱动器设为实例变量,如下所示,但仍然没有运气。任何建议都将不胜感激。

def authorize
  # Loads client secrets
  client_id = Google::APIClient::ClientSecrets.load

  # Assign it to authorization
  authorization = client_id.to_authorization

  # Create new Drive instance
  @drive = Google::Apis::DriveV3::DriveService.new

  @drive.authorization = authorization

  auth_client = authorization.dup

  auth_client.update!(
    :scope => 'https://www.googleapis.com/auth/drive',
    :approval_prompt => "force",
    :grant_type => "refresh_token"
  )
  auth_client.fetch_access_token!
end

def new
  file_metadata = {
    name: 'Hello World',
    mime_type: 'application/vnd.google-apps.folder'
  }
  file = @drive.create_file(file_metadata, fields: 'id')
  puts "Folder Id: #{file.id}"
end

0 个答案:

没有答案