Reputation: 3938
I'm trying to get my RSpec view tests to pass and am getting the above error. From searching, I believe it's a problem with nested routes, but I can't figure out how to fix it. Here is the full error:
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}
The line it's complaining about in the code is:
<ul class="cycles" data-update-url="<%= sort_cycles_program_url(params[:id]) %>" >
The test looks like:
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
and the route looks like this:
resources :programs do
member { post :sort_cycles }
resources :cycles_programs do
end
end
The sort_cycles action in the 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
EDIT:
Here is the full block of code from the view:
<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>
Upvotes: 2
Views: 1808
Reputation: 11509
The way you have written it, sort_cycles_program_url(params[:id])
will route to the sort_cycles
action within ProgramsController
. There could be a number of things failing here, so make sure you have them all correct:
sort_cycles
action defined within ProgramsController
sort_cycles.html.erb
template in views/programs
unless you are rendering a different template (It's unclear as to where your HTML code above is placed)sort_cycles
action can handle params[:id] == nil
, since that's what you're giving itUpvotes: 1