Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you have your <code>:cars</code> resource nested inside your <code>:users</code> resource:</p> <pre><code>resources :users do resources :cars do ... </code></pre> <p>If you structure your routes this way, then you need to specify a user as well when calling http actions. This won't work:</p> <pre><code>post :create, :car =&gt; @car </code></pre> <p>because it's missing a <code>:user_id</code>, which you access in the controller:</p> <pre><code>@user = User.find(params[:user_id]) </code></pre> <p>To solve this, pass a <code>:user_id</code> and then mock or stub <code>User</code> to return a user or mock user.</p> <p>UPDATE:</p> <p>It's not generally good practice to mix mocks and actual records, so here's how you could do it without mocks:</p> <pre><code>before(:each) do @user = User.create(...) @car = {:brand =&gt; "example", :color =&gt; "foobar", :model =&gt; "foobar", :year =&gt;"2012" } end it "should create a car" do lambda do post :create, :car =&gt; @car, :user_id =&gt; @user.id end.should change(Car, :count).by(1) end it "should redirect to the user welcome page" do post :create, :car =&gt; @car, :user_id =&gt; @user.id response.should redirect_to user_car_path end </code></pre> <p>Insert whatever minimal attributes you need to create a user into <code>User.create(...)</code>. I think that should do it.</p> <p>If you want to do this with mocks (which is generally better practice, to keep controller/model specs decoupled), here's <a href="http://www.vaporbase.com/postings/Rspec_example_for_nested_resource_index_action" rel="nofollow">a good starting point on how to do it</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload