2011-12-16 7 views
1

장치를 설치했습니다. 이것은 내가 응용 프로그램에있어 능력 클래스는cancan을 설정하려면 어떻게해야합니까?

rails g cancan:ability 

내가 한

,/모델

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    # Define abilities for the passed in user here. For example: 
    # 
    # user ||= User.new # guest user (not logged in) 
    # if user.admin? 
    #  can :manage, :all 
    # else 
    #  can :read, :all 
    # end 
    # 
    # The first argument to `can` is the action you are giving the user permission to do. 
    # If you pass :manage it will apply to every action. Other common actions here are 
    # :read, :create, :update and :destroy. 
    # 
    # The second argument is the resource the user can perform the action on. If you pass 
    # :all it will apply to every resource. Otherwise pass a Ruby class of the resource. 
    # 
    # The third argument is an optional hash of conditions to further filter the objects. 
    # For example, here the user can only update published articles. 
    # 
    # can :update, Article, :published => true 
    # 
    # See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities 
    end 
end 

게시물 테이블

        Table "public.posts" 
    Column |   Type   |      Modifiers      
-------------+------------------------+---------------------------------------------------- 
id   | integer    | not null default nextval('posts_id_seq'::regclass) 
title  | character varying(100) | not null 
content  | character varying(500) | not null 
created_at | date     | 
updated_at | date     | 
tags  | character varying(55) | not null default '50'::character varying 
category_id | integer    | not null default 1 
user_id  | integer    | 
Indexes: 
    "posts_pkey" PRIMARY KEY, btree (id) 

사용자 테이블

          Table "public.users" 
     Column   |   Type    |      Modifiers      
------------------------+-----------------------------+---------------------------------------------------- 
id      | integer      | not null default nextval('users_id_seq'::regclass) 
email     | character varying(255)  | not null default ''::character varying 
encrypted_password  | character varying(128)  | not null default ''::character varying 
reset_password_token | character varying(255)  | 
reset_password_sent_at | timestamp without time zone | 
remember_created_at | timestamp without time zone | 
sign_in_count   | integer      | default 0 
current_sign_in_at  | timestamp without time zone | 
last_sign_in_at  | timestamp without time zone | 
current_sign_in_ip  | character varying(255)  | 
last_sign_in_ip  | character varying(255)  | 
confirmation_token  | character varying(255)  | 
confirmed_at   | timestamp without time zone | 
confirmation_sent_at | timestamp without time zone | 
username    | character varying(255)  | not null 
is_admin    | boolean      | default false 
created_at    | timestamp without time zone | 
updated_at    | timestamp without time zone | 
Indexes: 
    "users_pkey" PRIMARY KEY, btree (id) 
    "index_users_on_confirmation_token" UNIQUE, btree (confirmation_token) 
    "index_users_on_email" UNIQUE, btree (email) 
    "index_users_on_reset_password_token" UNIQUE, btree (reset_password_token) 
    "index_users_on_username" UNIQUE, btree (username) 

이제 Cancan을 설정하여 PostController, CommentsController의 일부 작업을 허용/거부 할 수 있습니까? user.is_admin = true 사용자가 게시물, 댓글을 편집, 삭제할 수있는 경우. 그렇지 않으면 일반 사용자는 등록한 후에 만 ​​게시물을 추가 할 수 있습니다. 게스트 사용자는 모든 게시물에 대해 주석을 달 수 있습니다. 가 PostsController에서

가 나는 등의 라인을 작성했다 모든 컨트롤러에서
before_filter :authenticate_user! , :except => [:index, :show, :bla1, :bla2, :bla3, :bla4, :bla5, :bla6, :bla7, :bla8, :bla9] 

을 가지고, 그 지루한입니다. 모든 컨트롤러에서 이러한 라인을 줄이기위한 지름길 방법이 있습니까?

+1

허용 목록으로 만 사용할 수 있습니다. 'before_filter : authoenticate_user! : only => : create' – Gazler

+0

cancan 설정은 어떻습니까? 캔칸이 필요하지 않아? – shibly

답변

2

이 방법으로 ability.rb를 설정해야합니다.

class Ability 
    include CanCan::Ability 

    def initialize(user) 

    # rules for admin 
    if user.is_admin? 
     #if admin can do anything 
     can :manage, :all 
     #if admin can only edit and destroy posts and comments 
     can :edit, Post 
     can :destroy, Post 
     can :edit, Comment 
     can :destroy, Comment 
    end 

    #rules for registred user 
    can :create, Post 
    end 
end 

와의 당신의 컨트롤러

class PostsController < ApplicationController 
    authorize_resource :except => show 
end 

class CommentsController < ApplicationController 
    authorize_resource :only => [:edit,:update,:destroy] 
end 

은 도움이 될 것입니다 바랍니다. :)

관련 문제