2

我正在尝试让我的 RSpec 视图测试通过并收到上述错误。通过搜索,我认为这是嵌套路由的问题,但我不知道如何解决它。这是完整的错误:

programs/show
  renders attributes in <p> (FAILED - 1)

Failures:

  1) programs/show renders attributes in <p>
     Failure/Error: render
     ActionView::Template::Error:
       No route matches {:action=>"sort_cycles", :controller=>"programs", :id=>nil}

它在代码中抱怨的那一行是:

<ul class="cycles" data-update-url="<%= sort_cycles_program_url(params[:id]) %>" >

测试看起来像:

require 'spec_helper'

describe "programs/show" do
  before(:each) do
    FactoryGirl.create(:goal)
    FactoryGirl.create(:experience_level)
    @program = FactoryGirl.create(:program)
  end

  it "renders attributes in <p>" do
    render
    rendered.should match(/Name/)
    rendered.should match(/Gender/)
    rendered.should match(Goal.find(@program.goal_id).name)
    rendered.should match(ExperienceLevel.find(@program.experience_id).name)
  end
end

路线如下所示:

resources :programs do
    member { post :sort_cycles }
    resources :cycles_programs do
    end
  end

中的 sort_cycles 操作ProgramsController

def sort_cycles
    params[:cycles_program].each_with_index do |cycle_program_id, index|
      cycle_program = CyclesProgram.find(cycle_program_id)
      cycle_program.cycle_order = index+1
      cycle_program.save
    end
    render nothing: true
  end

编辑:

这是视图中的完整代码块:

<ul class="cycles" data-update-url="<%= sort_cycles_program_url(params[:id]) %>" >
  <% @program.cycles_programs.each do |program| %>
    <%= content_tag_for :li, program, class: "cycle-block" do %>
      <%= link_to program.cycle.name, program.cycle %> | <%= link_to "Remove", program_cycles_program_path(@program, program), method: :delete %>
    <% end %>
  <% end %>
</ul>
4

1 回答 1

1

您编写它的方式sort_cycles_program_url(params[:id])将路由sort_cyclesProgramsController. 这里可能有很多事情失败了,所以请确保它们都正确:

  1. 有一个sort_cycles动作定义在ProgramsController
  2. 有一个sort_cycles.html.erb模板,views/programs除非你渲染一个不同的模板(不清楚你上面的 HTML 代码放在哪里)
  3. 确保你的sort_cycles行动可以处理params[:id] == nil,因为那是你给它的
于 2013-10-10T16:27:40.263 回答