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