0

I'm working through Michael Hartl's excellent tutorial on Rails, but I am having trouble with exercise 7 in Chapter 11.

This exercise is:

Add a nested route so that /users/1/microposts shows all the microposts for user 1. (You will also have to add a Microposts controller index action and corresponding view.)

I've done this successfully by changing my routes.rb file to read:

resources :users do
  resources :microposts, :only => [:create, :destroy]
end

I am able to successfully call /users/1/microposts from a browser. However, most of the tests in microposts_controller_spec.rb are now broken. I receive the "no route matches" error when running autotest. For instance, the first test, which simply reads:

it "should deny access to 'create'" do
  post :create
  response.should redirect_to(signin_path)
end

now produces the following error:

1) MicropostsController access control should deny access to 'create' Failure/Error: post :create No route matches {:controller=>"microposts", :action=>"create"}

When I check rake routes

, I find this entry:

user_microposts POST   /users/:user_id/microposts(.:format)     {:action=>"create", :controller=>"microposts"}

which suggests the route does exist.

Has anyone else run into this issue while completing the tutorial? Is there a change I need to make in the spec file once I introduce nested routes? Does Rspec work with nested routes?

thanks

1 Answer 1

2

Because this is a nested route you will need to pass the user_id through:

some_user = way_of_creating_a_user_goes_here
post :create, :user_id => some_user.id

RSpec will attempt to go to the /microposts route without this parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer Ryan. I thought I tried this but evidently I didn't do it quite correctly - your solution works so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.