2014-06-11 68 views
0

我是rails新手,正在開發rails應用程序。 我已經安裝了Devise gem,並發現它在很多領域都很有用,但是我無法創建一個新用戶,除非我在Rails控制檯上執行它。我覺得這特別令人沮喪。有沒有人有任何建議或解決方案?在Rails 4中使用Devise註冊新用戶

當我註冊的僱員,我得到以下錯誤「康康舞::存取遭拒在JobsController#指數」

還要注意的是,當我驗證了謀害我的用戶模型,我已經列入「包括設計::型號:: DatabaseAuthenticatable」

我jobs_controller.rb文件

def index 
@users = User.all 
@user = current_user 

@jobs = Job.all 
@jobs = Job.paginate(:page => params[:page], :per_page => 3) 
end 

# GET /jobs/1 
# GET /jobs/1.json 
def show 
end 

# GET /jobs/new 
def new 
@job = Job.new(:date => Time.now, :date_of_loss => Time.now, :sign_date => Time.now, :time_called_in => Time.now) 
end 

# GET /jobs/1/edit 
def edit 
end 

# POST /jobs 
# POST /jobs.json 
def create 
@job = Job.new(job_params) 

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

    # PATCH/PUT /jobs/1 
    # PATCH/PUT /jobs/1.json 
    def update 
    respond_to do |format| 
    if @job.update(job_params) 
    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.destroy 
respond_to do |format| 
    format.html { redirect_to jobs_url } 
    format.json { head :no_content } 
end 
end 

private 
# Use callbacks to share common setup or constraints between actions. 
def set_job 
    @job = Job.find(params[:id]) 
end 

# Never trust parameters from the scary internet, only allow the white list through. 
def job_params 
    params.require(:job).permit(:date, :time_called_in, :date_of_loss, :contact, :job_address, :city, :postal_code, :site_phone, :work_phone, :cell_phone, :email, :referred_by, :insurance_company, :policy_number, :claim_number, :broker, :insurance_address, :insurance_city, :insurance_province, :insurance_postal_code, :insurance_phone, :insurance_contact, :insurance_email, :customer_name, :customer_address, :authorized, :signed_by_name, :sign_date, :signature, :contact_first_name, :contact_last_name) 
end 

我的能力模型看起來李柯本

class Ability 
include CanCan::Ability 
def initialize(user) 
user ||= User.new # guest user (not logged in) 
if user.role == "admin" 
    can :manage, :all 
elsif user.role == "manager" 
    can [:read, :update, :create], [Job, Equipment] 
    #can [:read, :update, :create], [Equipment] 
elsif user.role == "employee" 
    can [:read, :update, :create], [Equipment, Job] 
    cannot [:create], [Job] 
    #can [:read, :update], [Equipment] 

end 

+0

你的工作控制器是什麼樣的?看看這個:https://github.com/ryanb/cancan/issues/956 – ShivamD

+0

@ShivamD我將包括我的作業控制器的問題 –

+0

@ShivamD只包括我的作業控制器。謝謝 –

回答

0

如果您收到此錯誤:CanCan::AccessDenied in JobsController#index這是因爲你的用戶無權訪問JobsController#index方法,具有無關創建用戶。

當你說「註冊爲員工」時,你能解釋一下你的意思嗎?你在用戶表上有角色表或布爾值來表示用戶是僱員嗎?

請發佈您的Ability.rb類(可能在app/models/ability.rb中),因爲這是定義cancan的訪問控制的地方。

嘗試修改此:

user ||= User.new # guest user (not logged in) 

這樣:

user ||= User.new # guest user (not logged in) 
puts "user.role = #{user.role}" 

,然後檢查你的日誌,看看有什麼user.role的值。我想知道你的用戶是否沒有你認爲的角色。

+0

剛剛發佈了ability.rb類 –

+0

更新了我的問題,嘗試了一些東西。 – Catfish

+0

謝謝你的建議,我將嘗試 –

相關問題