【发布时间】:2014-10-15 21:42:24
【问题描述】:
刚开始熟悉 Rails,但这个让我很难过。感谢您的帮助!
这是当前失败的测试。
Failures:
1) SectionsController POST #create with valid attributes redirects to course_section_path
Failure/Error: expect(response).to redirect_to course_section_path
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"sections"} missing required keys: [:course_id, :id]
# ./spec/controllers/sections_controller_spec.rb:59:in `block (4 levels) in <top (required)>'
Finished in 0.12643 seconds (files took 1.77 seconds to load)
我尝试了很多方法来为重定向提供正确的参数,但似乎没有任何效果。帮助!!谢谢!
Rspec 测试
it "redirects to course_section_path" do
post :create, section: attributes_for(:section)
expect(response).to redirect_to course_section_path
end
控制器:sections#show、section#create 和强参数。
def create
@section = Section.new(section_params)
if @section.save
flash[:success]="New section added!"
redirect_to course_section_path(@section.course_id, @section.id)
else
flash[:error] = "There was an error creating the section."
render action: :new
end
end
def show
@course = Course.find(params[:course_id])
@section = @course.sections.find(params[:id])
end
private
def section_params
params.require(:section).permit(:name, :course_id)
end
工厂
factory :section do
name "Section One"
course_id 1
end
数据库架构
create_table "courses", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "sections", force: true do |t|
t.string "name"
t.integer "course_id"
t.datetime "created_at"
t.datetime "updated_at"
end
路线
resources :courses
resources :sections
resources :courses do
resources :sections
end
【问题讨论】:
标签: ruby-on-rails redirect rspec nested-routes