在我的應用程序中,我有以下資源Post和User。我希望用戶能夠更新他們自己的帖子和來賓帖子,並且我希望來賓能夠更新其他來賓帖子,但不是用戶。客人僅僅是一個非用戶(@post.user.blank
)。如何以當前用戶或非用戶身份更新資源?
Post Model
belongs_to :user
# columns: user_id, name, body
end
User Model
has_many :posts
end
我的控制器是我感到困惑,因爲我不知道如何使它所以更新後可以由當前用戶或訪客來完成,而不能讓每個人都更新任何人後。主要是另一個用戶更新其他用戶的帖子。
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render action: 'edit'
end
end
def post_params
params.require(:post).permit(:name, :body)
end
我在想這樣做的:
def update
@post = Post.find(params[:id])
if @post.user == current_user or @post.user.blank?
if @post.update(post_params)
redirect_to @post
else
render action: 'edit'
end
end
end
但我不知道如何安全的,這是。似乎用戶可以訪問其他用戶的帖子。
這是正確的嗎?我怎麼能這樣做?