我在创建控制器规范中遇到Id缺少nil错误。然而,我已经通过ID的东西,每次都面对同样的东西。这是我的创建规范和控制器文件
创建规范
describe 'POST :create' do
context 'with valid data' do
let(:valid_data) { FactoryGirl.attributes_for(:student) }
it 'redirect to show page' do
post :create, student: valid_data
expect(response).to redirect_to(student_path(assigns[:student]))
end
end
end
学生控制器
def create
@student = current_user.students.build(student_params)
respond_to do |format|
if @student.save
format.html { redirect_to @student }
format.json { render :show, status: :created, location: @student }
else
format.html { render :new }
format.json { render json: @student.errors, status: :unprocessable_entity }
end
end
end
private
def student_params
params.require(:student).permit(:Student_Prefix, :First_Name, :Middle_Name, :Last_Name, :Father_Prefix, :Father_Name, :Father_Middle_Name, :Father_Last_Name, :Mother_Prefix, :Mother_Name, :Mother_Middle_Name, :Mother_Last_Name, :user_id)
end
错误
1) StudentsController POST :create with valid data redirect to show page
Failure/Error: expect(response).to redirect_to(student_path(assigns[:student]))
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"students", :id=>nil} missing required keys: [:id]
# ./spec/controllers/students_controller_spec.rb:38:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
答案 0 :(得分:0)
save
StudentsController
失败,因此@student
没有ID。当您尝试在规范中构建路径时出现错误:
expect(response).to redirect_to(student_path(assigns[:student]))
assigns[:student]
没有ID。