gpt4 book ai didi

ruby-on-rails - 限制用户在相关模型的 Controller 中的访问

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:34 25 4
gpt4 key购买 nike

我有一个 Controller ,我可以在其中进行某种时髦的选择。我有一个属于一个用户的邀请表,并且有一个用户。

销毁邀请时,我希望“has_one User”进行销毁,在我的 Controller 中,我首先收到邀请用户的一系列邀请:

def destroy
@invitations = Invitation.find_by_recipient_email(current_user.email)

我想从这个@invitations 数组中使用 :id 参数进行查找。有没有办法做这样的事情:

@invitations = Invitation.find_by_recipient_email(current_user.email)
@invitation = @invitations.find(params[:id])

这样我就可以限制用户只能访问他们被邀请的邀请(使用 current_user 方法),然后选择特定的邀请。我目前无法执行此操作,因为 .find 不适用于数组。

感谢您的帮助/指点。

编辑:抱歉,我让帖子有点困惑,这里有更多信息:

这是我现在的整个销毁方法,我只想删除一条记录:

def destroy
@invitations = Invitation.find_by_recipient_email(current_user.email)
@invitation = @invitations.find(params[:id])

if @invitation.destroy
redirect_to invitations_path, :notice => "Declined invitation"
else
redirect_to :back
end
end

我的邀请对象看起来像:

Invitation(id: integer, list_id: integer, sender_id: integer, recipient_email: string, created_at: datetime, updated_at: datetime) 

其中 send_id 和 recipient_email 是两个不同的用户。

我的邀请.rb:

belongs_to :sender, :class_name => 'User'
has_one :recipient, :class_name => 'User'

也许问题是我会调用 current_users.invitations.find(params[:id]) 之类的东西并重做我的邀请模型?

最佳答案

你可以这样做:

invitatation_scope = Invitation.where(["recipient_email = ?",current_user.email])
@invitation = invitation_scope.find(params[:id])<br/>

但是你应该使用 before_filter:

before_filter :load_user_invitation, :only=>[:edit,:update,:destroy]

def load_user_invitation
@invitation = Invitation.where(["recipient_email = ?",current_user.email]).find(params[:id])
end

关于ruby-on-rails - 限制用户在相关模型的 Controller 中的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7336784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com