【问题标题】:Rails 4.1 nested routes redirect_to test won't passRails 4.1嵌套路由redirect_to测试不会通过
【发布时间】: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


    【解决方案1】:

    由于“sections”是“courses”中的嵌套资源,因此您需要提供 course_id 和 section_id 参数以及路径 course_section_path。试试这样的:

      it "redirects to course_section_path" do
            course = create(:course) #if you use Factories
            post :create, section: attributes_for(:section)
            expect(response).to redirect_to course_section_path(course_id: course.id, id: section.id)
      end
    

    【讨论】:

    • 好的,我正在尝试类似的方法,但我不断收到 - NameError: undefined local variable or method `section'
    • 顺便说一句,我认为您可以摆脱“资源:课程资源:部分”,因为您已经将它们声明为嵌套资源。所以,你的路线是这样的:资源:课程做资源:部分结束
    【解决方案2】:

    好的,我想出了这个!

    线

    post :create, section: attributes_for(:section)
    

    在数据库中创建一条记录,当分配给变量时,可用于测试。

    section = Section.last
    

    然后按照 Szymon Borucki 所说,我提供了 course_id 和调用路线所需的 Id。

    expect(response).to redirect_to course_section_path(course_id: section.course_id, id: section.id)
    

    它有效!

    这是整个工作测试!

    it "redirects to course_section_path" do
            post :create, section: attributes_for(:section)
            section = Section.last
            expect(response).to redirect_to course_section_path(course_id: section.course_id, id: section.id)
        end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 2011-08-21
      相关资源
      最近更新 更多