簡體   English   中英

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

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

我的ruby應用程序引發了一個突然出現的錯誤。 拋出的錯誤是JobsDevsController中的NoMethodError#listing => nil:NilClass的未定義方法`user_id'

我的代碼中在控制器中引發此錯誤的部分是

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

我的控制器

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

請幫忙這個senario

確保您為要列出的活動設置了set_job

您可能需要直接添加到列表操作中

@job = current_user.job

或將其添加到上市操作之前並考慮順序的更好方法

看起來您的is_authorised方法正在尋找@job ,它沒有在控制器中設置; 相反,您分配@jobs_dev

將方法更新為以下內容:

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

我不確定這是否足夠,因為您正在跳過在before_action設置:

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

似乎您需要從其中的except子句中刪除:listing

嘗試這兩種方法,它應該可以再次工作。 讓我知道您是否對此有任何疑問:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM