0

My Rails 应用程序是一个简单的应用程序,用户可以在其中注册和查看某个组织的工作。我已经设置了 Devise,以便用户必须经过身份验证才能执行某些操作,例如查看工作、发布工作等。我目前有一个工作模型和一个用户模型。我将如何设置权限,以便用户可以创建新帖子并且只能编辑和删除他们写的帖子?

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
4

2 回答 2

1

如果您打算扩展您的系统并添加其他角色,例如管理员、超级管理员、普通用户、访客等,那么我给您的建议是查看名为CanCan的 Ryan Bates 的身份验证 gem .

如果应用程序像您在回答中提到的那样简单,那么您还必须使用current_user设计 gem 提供的方法,您必须设置工作和用户之间的关系。我想这将是一个一对多的关系(每个用户可以有很多工作),这可以通过:

  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
    
于 2012-06-21T22:06:02.087 回答
0

有一个 gem 可以帮助你https://github.com/ryanb/cancan

还有一个截屏视频演示了它是如何工作的。http://railscasts.com/episodes/192-authorization-with-cancan

您可以使用 cancan 定义确定的用户可以执行的操作。

于 2012-06-21T20:53:09.433 回答