我的应用程序中有一个Rails Admin面板。当我尝试更新现有用户之一(编辑user_info字段+单击保存)时,我得到了
User failed to be updated
- Username has already been taken
即使我一点也不触摸用户名。
所有操作均通过管理员帐户进行。那已经can :manage, :all
在能力.rb
我已经尝试添加on: :create
到的末尾,validates :username
但是它给我抛出了一个不同的错误。
如何允许用户从Rails Admin仪表板进行编辑?
向上
validates :username, presence: true, uniqueness: true, length: {in: 3..150}
添加后 on: :create
ActiveRecord::StatementInvalid in RailsAdmin::MainController#edit
SQLite3::ConstraintException: PRIMARY KEY must be unique: UPDATE "users" SET "current_sign_in_at" = ?, "last_sign_in_at" = ?, "confirmed_at" = ?, "confirmation_sent_at" = ?, "user_info" = ?, "id" = ?, "updated_at" = ? WHERE "users"."id" = ?
def each
loop do
val = step
break self if done?
yield val
end
up2
如果我尝试编辑当前用户(我自己),则效果很好。
在针对经过身份验证的用户的能力课程中,我有以下内容:
can :update, User, id: user.id
将其更改为以下代码后,我可以编辑其他用户:
can :update, User do |u|
u.id == user.id
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句