簡體   English   中英

需要一些幫助來調試簡單的Rails未定義方法錯誤

[英]Need some help debugging simple Rails undefined method error

我們正在使用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有點陌生,並且很難找出可能導致此問題的原因。 如果有人有任何建議,我將不勝感激。

這是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM