【发布时间】:2015-04-08 09:11:50
【问题描述】:
我正在关注 RailsApps 的 Learn Ruby on Rails 教程,第 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
我的联系人控制器:
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 但它没有帮助
【问题讨论】:
-
有那个教程的链接
-
我会假设我们还没有拿到那本书,并从你的问题开始。该错误将您指向特定的代码行。那是哪条线?您是否将其包含在您的问题中?
-
嗨 Coderhs,是我最近买的一本书。 Max Williams,包括该行,在“contact.rb”中显示:“ss = connection.spreadsheet_by_title('Aprendo')”
-
我不知道是不是拼写错误,但你能修复
My contact.rb :文件equire "google_drive_v0"应该是require "google_drive_v0"
标签: ruby-on-rails ruby-on-rails-4 learn-ruby-on-rails