我想创建一个应用程序,用户可以在其中提名一个可以对其帐户具有只读访问权限的“密钥持有人”。我正在使用设计进行用户身份验证。此外,我在数据库中有布尔字段来确定登录的用户是否是密钥持有者,并且还有一个“access_id”整数字段,该字段等于允许他们访问的用户的用户 ID。
我正在尝试创建访问限制,以便只有用户或他们的密钥持有者才能访问他们的帐户(而不仅仅是在地址栏中输入其用户号码的任何人)。稍后我将根据用户角色进一步限制访问,但是我目前收到以下错误:
ActiveRecord::RecordNotFound in UsersController#myaccount
Couldn't find User without an ID
它指的是我添加到 application_controller.rb 的这段代码:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :correct_user
private
def correct_user
@user = User.find(params[:id])
@keyholder = User.find(params[:access_id])
redirect_to(root_path) unless current_user==@user||@keyholder
end
end
它给出了这一行的错误:
@keyholder = User.find(params[:access_id])
此外,这里是用户控制器中#myaccount 操作的代码:
def myaccount
if user_signed_in?
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
else
redirect_to root_path, notice: 'Please login to view your account'
end
end
我将数据库中所有记录的 access_id 设置为 0,除了密钥持有者,其中访问 id 等于允许他们访问的 user_id。
谁能帮我这个?在我进一步限制权限之前,我只希望合适的人能够访问这些帐户。谢谢!!
更新:
如果我将代码更改为:
def correct_user
@user = User.find(params[:id])
redirect_to(root_path) unless current_user==@user
end
那么用户只能访问他们自己的帐户,这很好,但我需要一种密钥持有者能够访问该帐户的方式。