请大家,我需要帮助解决此问题。我正在尝试从Rails控制台中获得工作/新工作;
NoMethodError - undefined method `jobs' for nil:NilClass:
app/controllers/jobs_controller.rb:17:in `new'
Rails.application.routes.draw do
resources :jobs
devise_for :users
resources :home
root 'jobs#index'
end
# GET /jobs/new
def new
@job = current_user.jobs.build
end
# POST /jobs
# POST /jobs.json
def create
@job = current_user.jobs.build(job_params)
respond_to do |format|
if @job.save
format.html { redirect_to @job, notice: 'Job was successfully created.' }
format.json { render :show, status: :created, location: @job }
else
format.html { render :new }
format.json { render json: @job.errors, status: :unprocessable_entity }
end
end
end
答案 0 :(得分:1)
我没有添加before_action:authenticate_user!在jobs_controller中。
我添加了它,现在可以使用了:
# jobs_controller
class JobsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
# stuffs
def method
end
def method
end
end