0

私はdeviseを使用しています

before_filter :authenticate_user! 

リソースページで、登録されたユーザーがページを作成/更新できるようになりましたが、特定のユーザー、たとえば:id = 22でthlsを持たないようにしたくありません。これを行う方法 ?

たとえば、これは PageController の私の更新コードです

@page = Page.find(params[:id])

だから私はそれを好きにしたい

if user.id != 22
    @page = Page.find(params[:id])
4

1 に答える 1

1

あなたのページコントローラでそれを行うことができます

if current_user and current_user.id != 22
  ...

またはbefore_filter、ページコントローラーで

before_filter :check_for_specific_user, :only => [:create, :update]

def check_for_specific_user
  if current_user and current_user.id == 22
    # redirect or something
  end
end

または、アプリケーションコントローラーで次のように実行することもできます

before_filter :check_for_specific_user

def check_for_specific_user
  if params[:controller] == 'pages' and ['create', 'update'].include? params[:action] and current_user and current_user.id == 22
    # redirect or something
  end
end

また、アプリケーション コントローラに次のようなメソッドを用意することをお勧めします。

def admin?
  current_user and current_user.id == 22
end

次に、ページコントローラーで、次のように書くことができます

if admin?
  # ...
end
于 2013-05-06T04:50:50.333 に答える