Testing Rails 4参数缺失,Rspec测试嵌套资源控制器更新方法
这里是rails的新成员 我理解这个错误,但我不明白为什么我的测试没有通过。我也不知道如何查看传递到操作中的params散列,因此调试对我来说有点混乱 失败测试错误Testing Rails 4参数缺失,Rspec测试嵌套资源控制器更新方法,testing,ruby-on-rails-4,rspec,controller,Testing,Ruby On Rails 4,Rspec,Controller,这里是rails的新成员 我理解这个错误,但我不明白为什么我的测试没有通过。我也不知道如何查看传递到操作中的params散列,因此调试对我来说有点混乱 失败测试错误 1) SectionsController PATCH #update with valid attributes finds the correct course.section assigns @section Failure/Error: patch :update, course_id: @section.cour
1) SectionsController PATCH #update with valid attributes finds the correct course.section assigns @section
Failure/Error: patch :update, course_id: @section.course_id, id: @section.id
ActionController::ParameterMissing:
param is missing or the value is empty: section
# ./app/controllers/sections_controller.rb:52:in `section_params'
# ./app/controllers/sections_controller.rb:36:in `update'
# ./spec/controllers/sections_controller_spec.rb:95:in `block (4 levels) in <top (required)>'
区段控制器更新方法
def update
@course = Course.find(params[:course_id])
@section = Section.find(params[:id])
if @section.update_attributes(section_params)
flash[:success] = "the section has been updated"
redirect_to [@course, @section]
else
flash[:error] = "you must enter the correct information"
render action: "edit"
end
end
private
def section_params
params.require(:section).permit(:name, :course_id)
end
end
路线
resources :courses do
resources :sections
end
模型
class Section < ActiveRecord::Base
belongs_to :course
validates :name, :course_id, presence: true
end
class Course < ActiveRecord::Base
has_many :sections
accepts_nested_attributes_for :sections
validates :name, presence: true
end
class部分
谢谢你的帮助 使用所有必需参数进行post的正确方法如下:
post :update, course_id: @section.course_id, id: @section.id, section: {name: @section.name,course_id: @section.course_id}
使用所有必需参数进行post的正确方法如下:
post :update, course_id: @section.course_id, id: @section.id, section: {name: @section.name,course_id: @section.course_id}
试试看它是否修复了:
post:update,course\u id:@section.course\u id,id:@section.id,section:{name:@section.name,course\u id:@section.course\u id}
!非常感谢你,很抱歉回复晚了,我整个周末都不在。所以我丢失了它看起来像的部分的属性?太好了,我会发布一个答案让你接受。试试看它是否修复:post:update,course\u id:@section.course\u id,id:@section.id,section:{name:@section.name,course\u id:@section.course\u id}
有效!非常感谢你,很抱歉回复晚了,我整个周末都不在。所以我遗漏了看起来像的部分的属性?太好了,我会发布一个答案让你接受。这并不能解决这个问题。问题是关于使用补丁的。POST用于新对象。修补程序用于增量PUT(更新),但不能解决此问题。问题是关于使用补丁的。POST用于新对象。修补程序用于增量PUT(更新)