【问题标题】:undefined method `encrypted_password=' for #<User:0x007fa414b913a0>#<User:0x007fa414b913a0> 的未定义方法 `encrypted_pa​​ssword='
【发布时间】:2015-11-20 08:20:14
【问题描述】:

我在 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>

请帮忙,谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby devise registration user-registration


    【解决方案1】:

    一切看起来都一团糟。 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 =&gt; false, :default =&gt; ""(其实应该是我上面提到的模型)?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多