需要一些帮助调试简单的 Rails 未定义方法错误



我们正在使用Rails4,Ruby2。

每当我访问应用程序上的注册时,它都会抛出一个错误:

undefined method `plan_id=' for #<User:0x007f0721517f28>

以下是完整的用户控制器:

http://hastebin.com/titisaweva.rb

以下是完整的用户模型:

http://hastebin.com/tuhokinabi.rb

下面是计划模型:

class Plan < ActiveRecord::Base
  has_many :users
end

错误表明用户控制器的第 16 行存在问题:

if !ENV['STRIPE_API_KEY'] || params[:coupon]
  @user.plan_id = Plan.find_by_stripe_id('free').id
end

我对rails有点陌生,很难弄清楚可能导致此问题的原因。 如果有人有任何建议,我将不胜感激。

这是

UsersController的第 15-17 行:

 if !ENV['STRIPE_API_KEY'] || params[:coupon]
   @user.plan_id = Plan.find_by_stripe_id('free').id
 end

您正在尝试为 plan_id 属性分配值,但该属性的字段在users表中不存在。您需要使用迁移添加此字段:

rails g migration add_plan_id_to_users plan_id:integer
rake db:migrate

最新更新