【发布时间】:2013-10-11 00:40:19
【问题描述】:
我正在尝试让我的 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 中的 sort_cycles 操作
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>
【问题讨论】:
-
rake routes向您显示sort_cycles路径的什么?
标签: ruby-on-rails rspec