Reputation: 249
My ruby application is throwing an error which has appeared all of a sudden. the error thrown is NoMethodError in JobsDevsController # listing=> undefined method `user_id' for nil:NilClass
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
Upvotes: 0
Views: 910
Reputation: 12203
Looks like your is_authorised
method is looking for @job
, which isn't set in your controller; rather, you assign @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 :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.
Try both of these things and it should work again. Let me know if you've any questions or have any issues with this :)
Upvotes: 0
Reputation: 437
Make sure you set_job for listing actiton
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
Upvotes: 1