【发布时间】:2018-09-17 13:52:09
【问题描述】:
我在为我的 Rails Web 应用程序创建单独的用户时遇到了问题。今天,我尝试关注 Zurora 对类似帖子的回答。 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 中正确更新。 重新创建表并再次运行迁移。 我还删除了架构文件并再次运行迁移以生成新的架构文件。
这些都没有奏效。
任何帮助将不胜感激。非常感谢
【问题讨论】:
标签: ruby-on-rails ruby postgresql roles