开发者

Ruby on Rails Tutorial, Chapter 11, Exercise 7 - Breaks my rspec tests

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜