未定义的方法错误,但我的方法是定义的?

时间:2014-01-21 00:02:00

标签: ruby-on-rails

当我运行我的应用程序时,我收到一个错误,指出:未定义的局部变量或方法`注册'用于#,但我不确定为什么会发生这种情况。根据下面的代码,Signup是我定义的一个新类。

感谢您的帮助!

控制器代码:

class SignupsController < ApplicationController

def new
    @signup = Signup.new
end

def create
    @signup = Signup.new(signup_params)
    if @signup.save
        signup.add_subscrip
    else
        redirect_to new_signup_path
    end
end

end

型号代码:

class Signup < ActiveRecord::Base

validates :email, presence: true, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i }
validates :name, presence: true, length: { maximum: 50 }

def add_subscrip
    connection = GoogleDrive.login(ENV['g_username'], ENV['g_password'])
    ss = connection.spreadsheet_by_title(ENV['spreadsheet_title'])
    ws = ss.worksheets[0]
    row = 3 + ws.num_rows 
    ws[row, 1] = self.name
    ws[row, 2] = Time.new
    ws[row, 3] = self.email
    ws.save
end

end

1 个答案:

答案 0 :(得分:0)

在您的创建方法

def create
  @signup = Signup.new(signup_params)
  if @signup.save
      signup.add_subscrip
  else
      redirect_to new_signup_path
  end
end

我的意思是

signup.add_subscrip 

需要

@signup.add_subscrip