2016-07-30 76 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方法。 –