简体   繁体   English

我的控制器未定义方法`user_id'中的nil:NilClass的Ruby-on-Rails NoMethodError

[英]Ruby-on-Rails NoMethodError in my controller undefined method `user_id' for nil:NilClass

My ruby application is throwing an error which has appeared all of a sudden. 我的ruby应用程序引发了一个突然出现的错误。 the error thrown is NoMethodError in JobsDevsController # listing=> undefined method `user_id' for nil:NilClass 抛出的错误是JobsDevsController中的NoMethodError#listing => nil:NilClass的未定义方法`user_id'

The part of my code that throws this error in my controller is 我的代码中在控制器中引发此错误的部分是

def is_authorised
      redirect_to root_path, alert: "You don't have permission..." unless current_user.id == @job.user_id
    end

My Controller 我的控制器

class JobsDevsController < ApplicationController
  before_action :set_jobs_dev , except: [:index, :new, :create, :show, :edit, :listing]
  before_action :authenticate_user!, except: [:show, :listing]
  before_action :is_authorised, only: [:listing, :budget, :description, :photo_upload, :location, :update, :show ]

  # GET /jobs_devs
  def index
    @jobs_devs = JobsDev.all
  end

  # GET /jobs_devs/1
  def show
  end

  # GET /jobs_devs/new
  def new
    @jobs_dev = current_user.jobs_devs.build
  end

  # def listing
  #   @jobs_dev = current_user.jobs_dev
  # end

  # GET /jobs_devs/1/edit
  def edit
  end

  def budget
  end

  # POST /jobs_devs
  def create
    @jobs_dev = current_user.jobs_devs.build(jobs_dev_params)

    if @jobs_dev.save!
      redirect_to listing_jobs_dev_path(@jobs_dev), notice: 'Jobs dev was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /jobs_devs/1
  # def update
  #   if @jobs_dev.update(jobs_dev_params)
  #     redirect_to @jobs_dev, notice: 'Jobs dev was successfully updated.'
  #   else
  #     render :edit
  #   end
  # end

  def update
  respond_to do |format|
    if @jobs_dev.update(jobs_dev_params)
      format.html { redirect_to @jobs_dev, notice: 'Post was successfully updated.' }
      format.json { render :show, status: :ok, location: @jobs_dev }
    else
      format.html { render :edit }
      format.json { render json: @jobs_dev.errors, status: :unprocessable_entity }
    end
  end
end

  # DELETE /jobs_devs/1
  def destroy
    @jobs_dev.destroy
    redirect_to jobs_devs_url, notice: 'Jobs dev was successfully destroyed.'
  end

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

    def is_authorised
      redirect_to root_path, alert: "You don't have permission..." unless current_user.id == @jobs_dev.user_id
    end


    # Only allow a trusted parameter "white list" through.
    def jobs_dev_params
      params.require(:jobs_dev).permit(:job_category, :job_type, :job_title, :job_description, :recurrence,
                                        :budget, images: []
      )
    end
end

Please can you help with this senario 请帮忙这个senario

Make sure you set_job for listing actiton 确保您为要列出的活动设置了set_job

You may need to add to the listing action directly 您可能需要直接添加到列表操作中

@job = current_user.job

or the better way to add it to before action of listing action and take order into consideration 或将其添加到上市操作之前并考虑顺序的更好方法

Looks like your is_authorised method is looking for @job , which isn't set in your controller; 看起来您的is_authorised方法正在寻找@job ,它没有在控制器中设置; rather, you assign @jobs_dev . 相反,您分配@jobs_dev

Update the method to the following: 将方法更新为以下内容:

def is_authorised
  redirect_to root_path, alert: "You don't have permission..." unless current_user.id == @jobs_dev.user_id
end

I'm not sure that's sufficient, as you're skipping setting this in your before_action : 我不确定这是否足够,因为您正在跳过在before_action设置:

before_action :set_jobs_dev , except: [:index, :new, :create, :show, :edit, :listing]

It looks as if you'll need to remove :listing from the except clause there. 似乎您需要从其中的except子句中删除:listing

Try both of these things and it should work again. 尝试这两种方法,它应该可以再次工作。 Let me know if you've any questions or have any issues with this :) 让我知道您是否对此有任何疑问:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoMethodError(nil:NilClass的未定义方法`user_id =&#39;):在文章控制器中 - NoMethodError (undefined method `user_id=' for nil:NilClass): in the article controller NoMethodError(nil:NilClass 的未定义方法“user_id”): - NoMethodError (undefined method `user_id' for nil:NilClass): Ruby on Rails 如何修复 NoMethodError: undefined method `id' for nil:NilClass - Ruby on Rails how to fix NoMethodError: undefined method `id' for nil:NilClass Rails:nil:NilClass的未定义方法`user_id =&#39; - Rails : undefined method `user_id=' for nil:NilClass Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass Ruby on Rails NoMethodError:nil:NilClass的未定义方法“ []” - Ruby on Rails NoMethodError: undefined method `[]' for nil:NilClass nil:NilClass Ruby on Rails的未定义方法&#39;&gt;&#39;(NoMethodError) - undefined method `>' for nil:NilClass Ruby on Rails (NoMethodError) nil 的未定义方法“%”:NilClass (NoMethodError) Ruby on Rails - Undefined method `%' for nil:NilClass (NoMethodError) Ruby on Rails Heroku ruby​​-on-rails部署“ nil:NilClass的未定义方法&#39;info&#39;” - Heroku ruby-on-rails deployment “undefined method `info' for nil:NilClass” 未定义的方法&#39;id&#39;为nil:NilClass,Ruby on Rails - Undefined method 'id' for nil:NilClass, Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM