2012-08-28 24 views
0

我有兩個型號用戶傳記,它們都有一對一的關聯。如何驗證用戶編輯只有有自己的傳記

每個用戶都有單一的傳記。我的問題是,我想讓用戶只編輯他們自己的傳記,但從URL一個用戶可以編輯其他人的傳記

EX「0.0.0.0:3000/biographies/8/edit」。這裏用戶可以很容易地改變ID。

我該如何阻止?

回答

0

在你biographies控制器,在「編輯」行爲(和更新,太)下,插入您的通話下面這個權利@biography = Biography.find(params[:id])

unless @biography.user == current_user 
    redirect_to root_path, notice: "You cannot do that" and return 
end 
+0

感謝bevanb您的快速response.is可能使用過濾器? –

+0

是的,在before_filter你會使用相同的代碼。但在這些行之前,你應該添加'@biography = Biography.find(params [:id])''。 – bevanb

+0

so:'before_filter:check_ownership,只有:[:edit,:update]',然後在控制器的底部創建你的'check_ownership'方法。 – bevanb

0
before_filter :fetch_bio, :only => [:edit, :update] 

def fetch_bio 
    @biography = current_user.biographies.find(params[:id]) 
end 
+0

謝謝sumskyi,你的回答幫助我很多解決問題。 –

相關問題