2014-06-11 66 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

谢谢你的建议,我将尝试 –

相关问题