Ruby on rails 当尝试登录我的网站时,我不断收到a:2错误禁止保存此用户:电子邮件不能为空,电子邮件不能为空
我正在尝试为我的网站创建用户,当注册该网站时,我希望用户信息进入我的后台。我安装了gem designe和gem CanCanCan,但当我尝试在注册页面注册时,它一直说有两个错误禁止保存此用户:电子邮件不能为空,电子邮件不能为空。我检查了类似的问题,但未能纠正我的问题 我在ruby on rails obv上 以下是我的迁移:Ruby on rails 当尝试登录我的网站时,我不断收到a:2错误禁止保存此用户:电子邮件不能为空,电子邮件不能为空,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试为我的网站创建用户,当注册该网站时,我希望用户信息进入我的后台。我安装了gem designe和gem CanCanCan,但当我尝试在注册页面注册时,它一直说有两个错误禁止保存此用户:电子邮件不能为空,电子邮件不能为空。我检查了类似的问题,但未能纠正我的问题 我在ruby on rails obv上 以下是我的迁移: # frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[
# frozen_string_literal: true
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
## Database authenticatable
t.string :email
t.string :encrypted_password
t.string :token
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
## 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
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
#冻结的字符串文字:true
类designeCreateUsers
这是我的user.rb:
class User < ApplicationRecord
has_secure_password
validates_presence_of :email
validates_uniqueness_of :email
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
class用户
以下是我的用户\u controller.rb:
class UsersController < ApplicationController
def user_params
params.require(:user).permit(:email, :password, :password_confirmation, :role)
end
end
class UsersController
注册
new.html.erb:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
注册
(最少字符)
如果我遗漏了什么,请告诉我,但我不这么认为,因为我是rails的新手,所以我可能是
谢谢
具有安全的密码
用于自定义身份验证,不必/不应与设备一起使用
删除用户
模型中的用户控制器
和电子邮件
验证。Desive为您处理所有这些
如果您想在注册时将Desive未考虑的其他属性(在您的情况下,它只是角色
)列入白名单,请将以下内容添加到应用程序控制器
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:role])
end
对于维护良好、经过战斗测试的gems,比如designe,阅读文档非常重要,除非您有充分的理由这样做,否则不要偏离文档 你有自定义注册视图吗?或者你正在使用Desive视图?当然!我在使用Deviate,我把注册new.html.erb放在问题中,问什么是
资源名称
?我实际上不知道资源名称代表什么,或者说实话,我现在正试图更好地理解Deviate,我将new.html.erb中的更改为{:action=>“savenew”}做| f |%>我现在被一个未定义的方法卡住了|不知道你为什么要这么做#正如我说的,坚持默认值,除非你有理由不这么做。所以我想我走的路是对的,但接下来的问题是,当我登录时说我无权访问此页面,这可能是后台的仪表板,这将是正常的,因为我可能是一个普通用户,只有管理员有权访问仪表板,但现在我不能回到登录页面,但我认为我应该能够单独解决我的问题,所以感谢您的帮助!