角色未添加到数据库中的用户

时间:2018-04-07 19:49:09

标签: ruby-on-rails ruby postgresql roles

我一直在为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中更新。 重新创建表并再次运行迁移。 我还删除了模式文件并再次运行迁移生成新的模式文件。

这些都没有奏效。

任何帮助将不胜感激。非常感谢你

2 个答案:

答案 0 :(得分:1)

您需要允许其他参数

let rec equivalent(a,b) = match a, b with
| [], [] -> true
| [], _
| _, [] -> false
| c::cc, d::dd -> if c = d then equivalent(cc,dd) else false;;

答案 1 :(得分:0)

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