我正在尝试让我的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
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>
答案 0 :(得分:1)
您编写它的方式,sort_cycles_program_url(params[:id])
将路由到sort_cycles
内的ProgramsController
操作。这里可能会有很多事情失败,所以一定要确保它们都正确无误:
sort_cycles
ProgramsController
行动
sort_cycles.html.erb
中设置views/programs
模板,除非您要呈现不同的模板(目前还不清楚上面的HTML代码放置位置)sort_cycles
操作可以处理params[:id] == nil
,因为这就是您提供的内容