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

时间:2013-09-17 23:37:46

标签: ruby-on-rails

我们正在使用Rails4,Ruby2。

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

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

这是完整的UsersController:

http://hastebin.com/titisaweva.rb

以下是完整的用户模型:

http://hastebin.com/tuhokinabi.rb

以下是计划模型:

class Plan < ActiveRecord::Base
  has_many :users
end

错误表明UsersController的第16行存在问题:

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

我对rails很新,我很难搞清楚导致这个问题的原因。如果有人有任何建议我会非常感激。

1 个答案:

答案 0 :(得分:2)

以下是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