Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: How to modify tests for a nested resource?
    primarykey
    data
    text
    <p>While learning Rails I've created an application with a <code>Domains</code> controller nested below a <code>Customers</code> controller. I'm using Rails 2.3.4 and it's been a learning experience. I managed to get the below routing set up:</p> <pre><code> customer_domains GET /customers/:customer_id/domains(.:format) {:controller=&gt;"domains", :action=&gt;"index"} POST /customers/:customer_id/domains(.:format) {:controller=&gt;"domains", :action=&gt;"create"} new_customer_domain GET /customers/:customer_id/domains/new(.:format) {:controller=&gt;"domains", :action=&gt;"new"} edit_customer_domain GET /customers/:customer_id/domains/:id/edit(.:format) {:controller=&gt;"domains", :action=&gt;"edit"} customer_domain GET /customers/:customer_id/domains/:id(.:format) {:controller=&gt;"domains", :action=&gt;"show"} PUT /customers/:customer_id/domains/:id(.:format) {:controller=&gt;"domains", :action=&gt;"update"} DELETE /customers/:customer_id/domains/:id(.:format) {:controller=&gt;"domains", :action=&gt;"destroy"} customers GET /customers(.:format) {:controller=&gt;"customers", :action=&gt;"index"} POST /customers(.:format) {:controller=&gt;"customers", :action=&gt;"create"} new_customer GET /customers/new(.:format) {:controller=&gt;"customers", :action=&gt;"new"} edit_customer GET /customers/:id/edit(.:format) {:controller=&gt;"customers", :action=&gt;"edit"} customer GET /customers/:id(.:format) {:controller=&gt;"customers", :action=&gt;"show"} PUT /customers/:id(.:format) {:controller=&gt;"customers", :action=&gt;"update"} DELETE /customers/:id(.:format) {:controller=&gt;"customers", :action=&gt;"destroy"} root / {:controller=&gt;"customers", :action=&gt;"index"} </code></pre> <p>However, all tests for the Domains controller are failing due to routing errors.</p> <p>For example the following test (generated by Rails' resource generator) fails, as do all other tests in the <code>DomainsControllerTest</code> class.</p> <pre><code>class DomainsControllerTest &lt; ActionController::TestCase test "should get index" do get :index assert_response :success assert_not_nil assigns(:domains) end end </code></pre> <p>It fails with the error:</p> <pre><code>No route matches {:action =&gt; "index", :controller =&gt; "domains"} </code></pre> <p>This makes sense since the default routes no longer exist and the Domains controller requires a <code>@customer</code> to be set. I've spent an afternoon looking for the needed change, but almost every site talks about Rspec tests instead of regular Rails tests.</p> <p>How do I modify the <code>domains_controller_test.rb</code> so it will understand the nested resource?</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.
 

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