【问题标题】:Role is not being added to users in database角色未添加到数据库中的用户
【发布时间】: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


    【解决方案1】:

    你需要允许额外的参数

    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
    

    【讨论】:

    • 我现在添加了它,但我似乎仍然遇到同样的问题。我也尝试再次重置数据库。还是不行
    • 没关系..我一直非常盲目和愚蠢。你是对的,我只需要在不同的控制器中定义它
    【解决方案2】:

    感谢 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多