gpt4 book ai didi

ruby-on-rails - 角色未添加到数据库中的用户

转载 作者:行者123 更新时间:2023-11-29 13:15:32 25 4
gpt4 key购买 nike

我一直在为我的 Rails Web 应用程序创建单独的用户时遇到问题。今天我试着按照 Zhurora 对类似帖子的回答。 https://stackoverflow.com/a/32159123

但是现在,当用户在注册页面上注册并从选项中选择角色时,该角色不会写入数据库。我已经按照指示设置了迁移,当我检查模式时它有一个角色条目。

但是它仍然没有被添加。这是用户在选择角色后注册时在终端中发生的情况。

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" = $1 LIMIT $2 [["email", "mytest@mytest.com"], ["LIMIT", 1]]
SQL (0.3ms) INSERT INTO "users" ("name", "email", "encrypted_password", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "gdfgdfg"], ["email", "mytest@mytest.com"], ["encrypted_password", "$2a$11$4qjIxrh1RO.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 文件

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 %>

在我尝试修复它时,我删除了所有表,认为它没有在 postgresql 中正确更新。重新创建表并再次运行迁移。我还删除了模式文件并再次运行迁移生成一个新的模式文件。

这些都没有用。

如有任何帮助,我们将不胜感激。非常感谢

最佳答案

你需要允许额外的参数

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

关于ruby-on-rails - 角色未添加到数据库中的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49711357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com