2012-06-21 28 views
0

我的Rails應用程序非常簡單,用戶可以在其中註冊和查看某個組織的作業。我已經設置了Devise,以便用戶在進行某些操作之前必須先進行身份驗證,例如查看作業,發佈作業等。我目前有一個工作模型和一個用戶模型。我將如何去設置權限,以便用戶可以創建新帖子並只編輯和刪除他們寫的帖子?Rails應用程序中的用戶權限

Job.rb: 

class Job < ActiveRecord::Base 

# Include default devise modules. Others available are: 
# :token_authenticatable, :confirmable, 
# :lockable, :timeoutable and :omniauthable 

    devise :database_authenticatable, 
      :recoverable, :rememberable, :trackable, :validatable 

# Setup accessible (or protected) attributes for your model 
attr_accessible :contact_email, :contact_phone, :description, :district, :due_date,  :expiration_date, :job_title, :posting_date, :requirements, :salary, :submission_process 

end 

User.rb 

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :confirmable, 
    # :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me 
    # attr_accessible :title, :body 
end 

Jobs_Controller.rb: 

class JobsController < ApplicationController 
before_filter :authenticate_user!, :except => [:show, :index] 


    # GET /jobs 
    # GET /jobs.json 
    def index 
    @jobs = Job.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @jobs } 
    end 
    end 

    # GET /jobs/1 
    # GET /jobs/1.json 
    def show 
    @job = Job.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @job } 
     end 
     end 

    # GET /jobs/new 
    # GET /jobs/new.json 
    def new 
    @job = Job.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @job } 
    end 
    end 

    # GET /jobs/1/edit 
    def edit 
    @job = Job.find(params[:id]) 
    end 

    # POST /jobs 
    # POST /jobs.json 
    def create 
    @job = Job.new(params[:job]) 

    respond_to do |format| 
     if @job.save 
     format.html { redirect_to @job, notice: 'Job was successfully created.' } 
     format.json { render json: @job, status: :created, location: @job } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @job.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /jobs/1 
    # PUT /jobs/1.json 
    def update 
    @job = Job.find(params[:id]) 

    respond_to do |format| 
     if @job.update_attributes(params[:job]) 
     format.html { redirect_to @job, notice: 'Job was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @job.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /jobs/1 
    # DELETE /jobs/1.json 
    def destroy 
    @job = Job.find(params[:id]) 
    @job.destroy 

    respond_to do |format| 
     format.html { redirect_to jobs_url } 
     format.json { head :no_content } 
    end 
    end 
end 
+0

看看[CanCan](https://github.com/ryanb/cancan)。 – sczizzo

回答

1

如果您打算擴展您的系統並添加其他角色,例如管理員,超級管理員,普通用戶,訪客等......那麼我的建議是查看瑞安貝茨的身份驗證寶石它被命名爲CanCan

如果該應用與您在答案中提到的一樣簡單,那麼您還必須使用由設計寶石提供的current_user方法,您必須在作業和用戶之間設置關係。我想這會是一個一對多的關係(每個用戶可以有很多工作)這可能是下降:

  1. 創建一個新的移民增加了一個列到jobs表中,列名應是user_id
  2. 用戶模型中添加has_many :jobs(user.rb)
  3. 作業模型添加belongs_to :user(job.rb)
  4. 在你的控制器的方法編輯,更新和銷燬應該是類似的東西

    def update 
        @job = current_user.jobs.find(params[:id]) 
        # the rest of the code is omitted 
        end 
    
        def destroy 
        @job = current_user.jobs.find(params[:id]) 
        @job.destroy 
        # the rest of the code is omitted 
        end 
    
        def edit 
        @job = current_user.jobs.find(params[:id]) 
        end 
    
+0

當帖子不是current_user的時候,你還應該隱藏鏈接來編輯和銷燬視圖。 –