0

私の 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という名前のRyanBatesの認証ジェムを確認することをお勧めします。 。

回答で述べたようにアプリが単純な場合はcurrent_user、devise gemが提供する方法も使用する必要があり、ジョブとユーザーの関係を設定する必要があります。私はそれが1対多の関係になると思います(各ユーザーは多くの仕事を持つことができます)これは次のようにダウンする可能性があります:

  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

あなたを助けることができる宝石がありますhttps://github.com/ryanb/cancan

それがどのように機能するかを示すスクリーンキャストもあります. http://railscasts.com/episodes/192-authorization-with-cancan

cancan を使用して、特定のユーザーが実行できるアクションを定義できます。

于 2012-06-21T20:53:09.433 に答える