NoMethodError(对于#<user> </user>,未定义的方法`password_digest ='

时间:2014-11-16 11:04:34

标签: ruby-on-rails heroku

在控制器中:

def createUserForHotel
  @user=User.new(params_createUserForHotel)
  if @user.save
    flash[:notice]="New user is created for the hotel"
    redirect_to(:controller=>'hotels',:action=>"createnewhotel")
  else
    flash[:notice]="New user not created "
    redirect_to(:controller=>'hotels',:action=>"createnewhotel")
  end
end

def params_createUserForHotel
  params.require(:user).permit(:username,:password,:email,:hotel_id)
end

在迁移中:

def up
  create_table :users do |t|
    t.string "first_name",:limit=>25
    t.string "last_name",:limit=>50
    t.string "email",:null=>false,:limit=>100
    t.string "username",:limit=>50  
    t.string "password",:limit=>40 
    t.integer "hotel_id"
    t.timestamps
  end

  add_index("users","hotel_id")
end

class AlterUsers < ActiveRecord::Migration
  def up          
    rename_column("users","password","hashed_password")   
  end

在Heroku日志中:

←[0m Started POST "/hotels/createUserForHotel" for 122.50.216.93 at 2014-11-16 10:18:16 +0000
←[36m2014-11-16T10:18:16.170638+00:00 app[web.1]:←[0m
←[36m2014-11-16T10:18:16.170641+00:00 app[web.1]:←[0m NoMethodError (undefined method password_digest=' for #<User:0x007f036c8ffe50>):
←[36m2014-11-16T10:18:16.170643+00:00 app[web.1]:←[0m   app/controllers/hotels_controller.rb:140:in createUserForHotel'
←[36m2014-11-16T10:18:16.170644+00:00 app[web.1]:←[0m

我的应用程序在开发环境中工作得很好,但是我在Heroku日志中遇到了上述错误。我已经在Heroku中部署了我的应用程序。

4 个答案:

答案 0 :(得分:4)

您的password列应命名为password_digest - 而不是hashed_passwordpassword

答案 1 :(得分:0)

迁移不会在部署时自动执行。确保在Heroku上运行rake db:migrate

 heroku run rake db:migrate

请参阅此Rails 4 on Heroku guide

答案 2 :(得分:0)

作为选项,您可以委派(或存根)

class User < ApplicationRecord
  def password_digest=(hashed_psw)
    hashed_password = hashed_psw
  end
end

P.S。如果您创建列password_digest

会更好

答案 3 :(得分:0)

您可以在用户模型中使用center