用户下的嵌套资源,我只希望current_user能够访问

时间:2016-07-30 14:43:17

标签: ruby-on-rails

我的routes.rb下面有以下内容:

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

我只希望current_user(登录用户)能够根据submitted_termsindex视图查看自己的show。他们不应该看到任何其他人的indexshow观点,其他人也不应该看到他们的观点。

我想我知道如何实现这一点但对我来说感觉有些混乱。有什么想法吗?

1 个答案:

答案 0 :(得分: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

您可能需要更改代码或创建新操作(过滤器)以检查用户是否已登录