2016-07-30 29 views
0

我已經欠我routes.rb如下:嵌套的資源,我只想要CURRENT_USER才能夠訪問

resources :users do 
    resources :submitted_terms, only: [:index, :create, :show] 
    end 

我只希望current_user(登錄的用戶)才能看到自己的根據indexshow的意見,擁有submitted_terms。他們不應該能夠看到其他人的indexshow意見,而其他人不應該能夠看到他們的意見。

我想我知道如何實現這一點,但它感覺有點亂。有什麼想法嗎?

回答

0

您可以before_action過濾器。

before_action :correct_user, only: [#action for which you need this filer] 

    def correct_user 
    @submitted_term = SubmittedTerm.find(params[:id]) 
    unless @submitted_term.user == current_user 
     flash[:notice] = "Insuffient privlage" 
     redirect_to #some path or render 
    end 
    end 

您可能需要更改代碼或創建新的動作(過濾器)檢查,如果用戶登錄或不

+0

是啊,這就是我所做的,但如果是在嵌套的路線,我認爲你需要做一些類似'@user = SubmittedTerm.find(params [:user_id])。user'。 –

+0

我編輯了我的答案。看看是否可行 –

+0

你應該使用SubmittedTerm.find(params [:id])。用戶bcoz你正在對SubmittedTerm調用find方法。 –