ContactsController 中的 NoMethodError#create

NoMethodError in ContactsController#create

我正在尝试制作一个功能性联系表,将数据存储在 Google Drive 电子表格中。 当我测试应用程序并填写联系表并按 'submit' 时,我收到此错误:

NoMethodError in ContactsController#create  
undefined method `login' for GoogleDrive:Module.

应用程序跟踪

app/models/contact.rb:16:in 'update_spreadsheet'  
app/controllers/contacts_controller.rb:10:in `create'

contact.rb

class Contact
  include ActiveModel::Model
  attr_accessor :name, :string
  attr_accessor :email, :string
  attr_accessor :content, :string
  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :content
  validates_format_of :email,
  :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
  validates_length_of :content, :maximum => 500

  def update_spreadsheet

    connection = GoogleDrive.login(Rails.application.secrets.email_provider_username,
      Rails.application.secrets.email_provider_password
      )

    ss = connection.spreadsheet_by_title('Learn-Rails-Example')
    if ss.nil?
      ss = connection.create_spreadsheet('Learn-Rails-Example')
    end 
    ws = ss.worksheets[0]
    last_row = 1 + ws.num_rows
    ws[last_row, 1] = Time.new
    ws[last_row, 2] = self.name
    ws[last_row, 3] = self.email
    ws[last_row, 4] = self.content
    ws.save
  end

end

contacts_controller.rb

class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end  

  def create
    @contact = Contact.new(secure_params)
    if @contact.valid?
            @contact.update_spreadsheet
      # TODO send message
      flash[:notice] = "Message sent from #{@contact.name}."
      redirect_to root_path
    else
      render :new
    end
  end

  private
  def secure_params
    params.require(:contact).permit(:name, :email, :content)
  end
end

您需要使用:GoogleDrive.login_with_oauth

def update_spreadsheet

  connection = GoogleDrive.login_with_oauth(access_token)
  )
...

end

获得 access_token

# Authorizes with OAuth and gets an access token.
client = Google::APIClient.new
auth = client.authorization
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
    "https://www.googleapis.com/auth/drive " +
    "https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:\n%s\n\n" % auth.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token

你可以做第二种方法,像这样

这是与您遇到的问题相关的摘录。

版本。 1.0.0 不是 100% 向后兼容 0.3.x。一些方法已被删除。特别是,GoogleDrive.login 已被删除,您必须使用 GoogleDrive.login_with_oauth 代替,如以下示例代码所示。

在此处阅读更多内容:https://github.com/gimite/google-drive-ruby

更新:

您可以使用新 class

实现新文件

或者只是在某处添加一个新方法:

def new_access_token
  client = Google::APIClient.new
  ... #excluded some code
  access_token = auth.access_token
  access_token # this line important, returning access_token
end

现在你可以在里面调用pass了,像这样:connection = GoogleDrive.login_with_oauth(new_access_token)

如果您想创建一个新的 class,请执行以下操作:

Class Token
  def new_access_token
  ...
  end
end

这样做可能更简洁,现在您可以通过以下方式调用它:

token = Token.new
token.new_access_token

并将其传入:

GoogleDrive.login_with_oauth(token.new_access_token)

这取决于您喜欢哪种方法。

我在学习 Daniel Kehoe 的 Learn Rails 一书时也遇到了这个问题。如果您检查 GitHub repository for the project,他包含了一个简单的解决方法。具体来说:

在文件中app/models/contact.rb

require "google_drive_v0"

以及连接变量:

connection = GoogleDriveV0.login(Rails.application.secrets.email_provider_username, Rails.application.secrets.email_provider_password)

已从书中更新。该文件的完整代码是:

require "google_drive_v0"
class Contact
  include ActiveModel::Model
  attr_accessor :name, :string
  attr_accessor :email, :string
  attr_accessor :content, :string

  validates_presence_of :name
  validates_presence_of :email
  validates_presence_of :content
  validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
  validates_length_of :content, :maximum => 500

  def update_spreadsheet
    connection = GoogleDriveV0.login(Rails.application.secrets.email_provider_username, Rails.application.secrets.email_provider_password
)
    ss = connection.spreadsheet_by_title('Learn-Rails-Example')
    if ss.nil?
      ss = connection.create_spreadsheet('Learn-Rails-Example')
    end
    ws = ss.worksheets[0]
    last_row = 1 + ws.num_rows
    ws[last_row, 1] = Time.new
    ws[last_row, 2] = self.name
    ws[last_row, 3] = self.email
    ws[last_row, 4] = self.content
    ws.save
  end

end

这将允许您的代码 运行 使用旧方法,但您会在控制台中收到以下警告:

WARNING: GoogleDriveV0.login is deprecated and will be removed in the next version. Use GoogleDriveV0.login_with_oauth instead.

要解决此问题,您可以按照其他张贴者提供的有用信息进行操作。希望这对您有所帮助!