角色未添加到数据库中的用户
Role is not being added to users in database
我在为我的 rails 网络应用程序创建单独的用户时遇到了问题。今天我试着按照 Zhurora 的回答类似 post。
但是现在,当用户在注册页面上注册时,select 从选项中选择一个角色,该角色不会写入数据库。我已经按照指示设置了迁移,当我检查模式时它有一个角色条目。
但是它仍然没有被添加。这是用户在 select 担任角色后注册时在终端中发生的情况。
Started POST "/users" for 127.0.0.1 at 2018-04-07 19:54:54 +0200
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"XKuxBEDRnYGOMMJXP1hb8HfkHkVc4WrMddq13WCqnXW//g6R+yzZ7DB8NTBkDgKsJReIcg83gkmfYaTEpmq+iQ==", "user"=>{"name"=>"gdfgdfg", "email"=>"Mytest@Mytest.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"restaurant"}, "commit"=>"Sign up"}
Unpermitted parameter: :role
(0.1ms) BEGIN
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = LIMIT [["email", "mytest@mytest.com"], ["LIMIT", 1]]
SQL (0.3ms) INSERT INTO "users" ("name", "email", "encrypted_password", "created_at", "updated_at") VALUES (, , , , ) RETURNING "id" [["name", "gdfgdfg"], ["email", "mytest@mytest.com"], ["encrypted_password", "aqjIxrh1RO.CL7FEPoVFs.xa712fALq4ayFMIC/8taGmU6iUWRphe"], ["created_at", "2018-04-07 17:54:54.479270"], ["updated_at", "2018-04-07 17:54:54.479270"]]
(428.6ms) COMMIT
这是我的 user.rb 模型
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
enum role: {person: 0, restaurant: 1, admin: 2}
has_many :posts, dependent: :destroy
validates :role, presence: true
validates :name, presence: true, length: { minimum: 4 }
validates :password, presence: true, length: { minimum: 5 }
validates :password_confirmation, presence: true, length: { minimum: 5 }
end
我的 schema.rb 用户文件 table
create_table "users", force: :cascade do |t|
t.string "name", default: "", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
迁移文件
class AddRoleToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :role, :integer
end
end
还有我的注册html.erb页面
<%= bootstrap_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.text_field :name,
placeholder: 'username (will be shown publicly)',
class: 'form-control' %>
<%= f.text_field :email,
placeholder: 'email',
class: 'form-control' %>
<%= f.password_field :password,
placeholder: 'password',
class: 'form-control' %>
<%= f.password_field :password_confirmation,
placeholder: 'password confirmation',
class: 'form-control' %>
<%= f.select :role, collection: User.roles.keys.to_a %>
<%= f.submit 'Sign up', class: 'btn sign-up-button' %>
<% end %>
在我尝试修复它时,我删除了所有 tables,认为它没有在 postgresql 中正确更新。
重新创建了 tables 和 运行 迁移。
我还删除了架构文件,运行 迁移再次生成一个新的架构文件。
None 已经奏效了。
如有任何帮助,我们将不胜感激。非常感谢
您需要允许额外的参数
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:role])
end
end
感谢 Mezbah,我研究了向允许的参数添加角色。
我被蒙蔽了双眼而忽略了这一点。
我用自己的
覆盖了注册控制器
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation)
end
def account_update_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation,
:current_password)
end
end
这是以前的样子。
然后根据 Mezbah 所说,我将其添加到此处
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation,
:role)
end
def account_update_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation,
:current_password)
end
结束
现在可以了:D
这里是成功进入数据库
Started POST "/users" for 127.0.0.1 at 2018-04-07 22:45:24 +0200
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ccL1AgCfT8WfRv7ObfF3INJ8bNcpQ1/WVDBxtX3CAUAW1Qsq+T3ndoYGMikOgrkWuUZmA9vtv2FS70+zTnwzmw==", "user"=>{"name"=>"MyTest", "email"=>"Mytest@Mytest.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"restaurant"}, "commit"=>"Sign up"}
(0.1ms) BEGIN
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = LIMIT [["email", "mytest@mytest.com"], ["LIMIT", 1]]
SQL (0.3ms) INSERT INTO "users" ("name", "email", "encrypted_password", "created_at", "updated_at", "role") VALUES (, , , , , ) RETURNING "id" [["name", "MyTest"], ["email", "mytest@mytest.com"], ["encrypted_password", "a$isG5sdUAJmmtKCcgt4oZ1.HP6IPT4F2oPOFWWMi.raYJLsbuhcnPC"], ["created_at", "2018-04-07 20:45:24.216616"], ["updated_at", "2018-04-07 20:45:24.216616"], ["role", 1]]
(19.3ms) COMMIT
我在为我的 rails 网络应用程序创建单独的用户时遇到了问题。今天我试着按照 Zhurora 的回答类似 post。
但是现在,当用户在注册页面上注册时,select 从选项中选择一个角色,该角色不会写入数据库。我已经按照指示设置了迁移,当我检查模式时它有一个角色条目。
但是它仍然没有被添加。这是用户在 select 担任角色后注册时在终端中发生的情况。
Started POST "/users" for 127.0.0.1 at 2018-04-07 19:54:54 +0200
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓","authenticity_token"=>"XKuxBEDRnYGOMMJXP1hb8HfkHkVc4WrMddq13WCqnXW//g6R+yzZ7DB8NTBkDgKsJReIcg83gkmfYaTEpmq+iQ==", "user"=>{"name"=>"gdfgdfg", "email"=>"Mytest@Mytest.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"restaurant"}, "commit"=>"Sign up"}
Unpermitted parameter: :role
(0.1ms) BEGIN
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = LIMIT [["email", "mytest@mytest.com"], ["LIMIT", 1]]
SQL (0.3ms) INSERT INTO "users" ("name", "email", "encrypted_password", "created_at", "updated_at") VALUES (, , , , ) RETURNING "id" [["name", "gdfgdfg"], ["email", "mytest@mytest.com"], ["encrypted_password", "aqjIxrh1RO.CL7FEPoVFs.xa712fALq4ayFMIC/8taGmU6iUWRphe"], ["created_at", "2018-04-07 17:54:54.479270"], ["updated_at", "2018-04-07 17:54:54.479270"]]
(428.6ms) COMMIT
这是我的 user.rb 模型
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
enum role: {person: 0, restaurant: 1, admin: 2}
has_many :posts, dependent: :destroy
validates :role, presence: true
validates :name, presence: true, length: { minimum: 4 }
validates :password, presence: true, length: { minimum: 5 }
validates :password_confirmation, presence: true, length: { minimum: 5 }
end
我的 schema.rb 用户文件 table
create_table "users", force: :cascade do |t|
t.string "name", default: "", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
迁移文件
class AddRoleToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :role, :integer
end
end
还有我的注册html.erb页面
<%= bootstrap_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= f.text_field :name,
placeholder: 'username (will be shown publicly)',
class: 'form-control' %>
<%= f.text_field :email,
placeholder: 'email',
class: 'form-control' %>
<%= f.password_field :password,
placeholder: 'password',
class: 'form-control' %>
<%= f.password_field :password_confirmation,
placeholder: 'password confirmation',
class: 'form-control' %>
<%= f.select :role, collection: User.roles.keys.to_a %>
<%= f.submit 'Sign up', class: 'btn sign-up-button' %>
<% end %>
在我尝试修复它时,我删除了所有 tables,认为它没有在 postgresql 中正确更新。 重新创建了 tables 和 运行 迁移。 我还删除了架构文件,运行 迁移再次生成一个新的架构文件。
None 已经奏效了。
如有任何帮助,我们将不胜感激。非常感谢
您需要允许额外的参数
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:role])
end
end
感谢 Mezbah,我研究了向允许的参数添加角色。
我被蒙蔽了双眼而忽略了这一点。
我用自己的
覆盖了注册控制器class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation)
end
def account_update_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation,
:current_password)
end
end
这是以前的样子。
然后根据 Mezbah 所说,我将其添加到此处
class RegistrationsController < Devise::RegistrationsController
private
def sign_up_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation,
:role)
end
def account_update_params
params.require(:user).permit( :name,
:email,
:password,
:password_confirmation,
:current_password)
end
结束
现在可以了:D
这里是成功进入数据库
Started POST "/users" for 127.0.0.1 at 2018-04-07 22:45:24 +0200
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ccL1AgCfT8WfRv7ObfF3INJ8bNcpQ1/WVDBxtX3CAUAW1Qsq+T3ndoYGMikOgrkWuUZmA9vtv2FS70+zTnwzmw==", "user"=>{"name"=>"MyTest", "email"=>"Mytest@Mytest.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "role"=>"restaurant"}, "commit"=>"Sign up"}
(0.1ms) BEGIN
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = LIMIT [["email", "mytest@mytest.com"], ["LIMIT", 1]]
SQL (0.3ms) INSERT INTO "users" ("name", "email", "encrypted_password", "created_at", "updated_at", "role") VALUES (, , , , , ) RETURNING "id" [["name", "MyTest"], ["email", "mytest@mytest.com"], ["encrypted_password", "a$isG5sdUAJmmtKCcgt4oZ1.HP6IPT4F2oPOFWWMi.raYJLsbuhcnPC"], ["created_at", "2018-04-07 20:45:24.216616"], ["updated_at", "2018-04-07 20:45:24.216616"], ["role", 1]]
(19.3ms) COMMIT