ruby-on-rails - RoR - ContactsController 中的 NoMethodError

标签 ruby-on-rails ruby-on-rails-4 learn-ruby-on-rails

我正在关注 Learn Ruby on Rails来自 RailsApps 的教程,第 22 章“电子表格连接”。

按照书本和 git 所示完成所有操作后,我收到此错误

NoMethodError in ContactsController#create undefined method `new' for
#<String:0x00000004fe5778> Extracted source (around line #19): 17 18 19 20 21 22

    connection = GoogleDriveV0.login_with_oauth(Rails.application.secrets.email_provider_username, Rails.application.secrets.email_provider_password )
    ss = connection.spreadsheet_by_title('Aprendo')
    if ss.nil?
      ss = connection.create_spreadsheet('Aprendo')
    end

Rails.root: /home/action/workspace/aprendo

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

我不知道它会是什么。

我的联系人.rb:

equire "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_with_oauth(Rails.application.secrets.email_provider_username, Rails.application.secrets.email_provider_password
)
    ss = connection.spreadsheet_by_title('Aprendo')
    if ss.nil?
      ss = connection.create_spreadsheet('Aprendo')
    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

我的联系人 Controller :

class ContactsController < ApplicationController

  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(secure_params)
    if @contact.valid?
      @contact.update_spreadsheet
      UserMailer.contact_email(@contact).deliver
      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

正如 git 书中所说,我更改了 Secrets.yml 但没有帮助

最佳答案

您需要使用: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

您可以使用新类实现新文件

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

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 Token
  def new_access_token
  ...
  end
end

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

token = Token.new
token.new_access_token

并将其传递给:

GoogleDrive.login_with_oauth(token.new_access_token)

关于ruby-on-rails - RoR - ContactsController 中的 NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29510471/

相关文章:

ruby-on-rails - Rails 错误消息和重定向

ruby-on-rails - 安装 ruby​​-opencv (0.0.14) 时出错

ruby-on-rails - Rails 无法识别路径

ruby-on-rails - 无法安装 rails - "File exists @ dir_s_mkdir"错误

ruby-on-rails - ApplicationController 方法的 Rails 作用域

ruby-on-rails - 如何调试 AWS S3 "Access Denied"

ruby-on-rails - rake 分贝 :seed is not working using neo4j gem

mysql - 使用 Rails 4 中的 include 优化查询

learn-ruby-on-rails - Rails 应用中的环境变量为零(无法连接到 Google Drive)