#<User:0x007fa414b913a0> 的未定义方法 `encrypted_password='

undefined method `encrypted_password=' for #<User:0x007fa414b913a0>

我在 Devise::RegistrationsController#create 中遇到了这个错误 NoMethodError,我不知道我是如何修复它的。如果我尝试进行新的注册,服务器会加载很长时间,之后我会看到这个错误。

我的迁移是这样的:

class DeviseCreateRegistrationsControllers < ActiveRecord::Migration
  def change
    create_table(:registrations_controllers) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
       #t.string :encrypted_password, :null => false, :default => ""
      t.database_authenticatable :null => false
      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :registrations_controllers, :email,                unique: true
    add_index :registrations_controllers, :reset_password_token, unique: true
    # add_index :registrations_controllers, :confirmation_token,   unique: true
    # add_index :registrations_controllers, :unlock_token,         unique: true
  end
end

我的注册控制器是这样的:

class RegistrationsController < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  t.string :encrypted_password, :null => false, :default => ""
end

我的 views/devise/registration/new.html.erb 看起来像这样

<div class="form-container glassy-bg small-10 small-centered medium-8 large-6 columns">
  <h2>Register</h2>

  <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <div class="mb1"><%= f.email_field :email, autofocus: true, placeholder: "Email", class: "radius" %></div>

    <div class="mb1"><%= f.password_field :password, autocomplete: "off", placeholder: "Password", class: "radius" %></div>

    <div class="mb1"><%= f.password_field :password_confirmation, autocomplete: "off", placeholder: "Confirm password", class: "radius" %></div>

    <div><%= f.submit "Let's Go", class: "button" %></div>
  <% end %>
  <%= render "devise/shared/links" %>
</div>

请帮忙,谢谢。

一切看起来一团糟。 RegistrationsController 应该是模型 User 因为它扩展了 ActiveRecord::Base 和那些行:

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

同样来自用户模型的设计。

为什么你的RegistrationsController里面有t.string :encrypted_password, :null => false, :default => ""(其实应该是我上面说的模型)?

一定是某处复制粘贴错误。请再次检查整个项目或重新创建新的 Rails 应用程序,然后从干净的项目安装 Devise。