2

I'm making a simple test project to prepare myself for my test. I'm fairly new to nested resources, in my example I have a newsitem and each newsitem has comments.

The routing looks like this:

resources :comments

resources :newsitems do
    resources :comments
end

I'm setting up the functional tests for comments at the moment and I ran into some problems.

This will get the index of the comments of a newsitem. @newsitem is declared in the setup ofc.

test "should get index" do
    get :index,:newsitem_id => @newsitem
    assert_response :success
    assert_not_nil assigns(:newsitem)
end

But the problem lays here, in the "should get new".

 test "should get new" do
    get new_newsitem_comment_path(@newsitem)
    assert_response :success
 end

I'm getting the following error.

ActionController::RoutingError: No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}

But when I look into the routes table, I see this:

new_newsitem_comment GET    /newsitems/:newsitem_id/comments/new(.:format)      {:action=>"new", :controller=>"comments"}

Can't I use the name path or what I'm doing wrong here?

Thanks in advance.

2 Answers 2

7

The problem is in the way your test specifies the URL. The error message is:

No route matches {:controller=>"comments", :action=>"/newsitems/1/comments/new"}

and of course there is no action called "/newsitems/1/comments/new". You want to pass the hash { :controller => :comments, :action => :new, :news_item_id => 1 }.

The right syntax is simply:

get :new, :news_item_id => 1
Sign up to request clarification or add additional context in comments.

1 Comment

I was having a similar issue with functional testing where I had many nested routes. This helped immensely! Thanks @zetetic
0

(Assuming Rails 3)

Try this in your routes.rb

GET    'newsitems/:newsitem_id/comments/new(.:format)' => 'comments#new', :as => :new_newsitem_comment

2 Comments

Still the same problemen. I'm using get :index,{:controller => "/comments",:newsitem_id => @newsitem} now and it doesnt return any errors. But this will just get me to the host:port/comments/new right? But it shouldn't be a problem since I give the newsitem with it right? Just find it so strange why I cant use the paths :s
Given you actually specify nested resources in routes.rb, I think you should remove that line completely. Rails should have set up a named path for you. Check the output of rake routes, but it should be something like new_newsitem_comment

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.