我正在尝试让我的 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>