# Nested Resource - "You are Being Redirected" interim page

**URL:** <https://www.ruby-forum.com/t/nested-resource-you-are-being-redirected-interim-page/138154>\
**Category:** Rails\
**Created:** [May 20, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/nested-resource-you-are-being-redirected-interim-page/138154 "2008-05-20T17:17:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![emh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@emh](https://www.ruby-forum.com/u/emh)\
**Post date:** [May 20, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/nested-resource-you-are-being-redirected-interim-page/138154/1 "2008-05-20T17:17:12Z")

</div>

Hi,

A bit of a noob andtrying to get RESTful nested resources working for  
the first time.  
My environments is as follows:  
Mac Leopard  
Ruby 1.8.6  
Rails 2.0.2 - standard everything, mongrel, etc.

Following various instructions & tutorials, I’ve got this working 99%  
but apparently have made an oversight which I can’t figure out that’s  
causing some problems.

* * *

My resources are Customers & Contacts  
routes.rb looks like:

## map.resources :customers do |customer| customer.resources :contacts end

My contact controller includes these changes:

class ContactsController \< ApplicationController  
before\_filter :load\_parent

def create  
@contact = Contact.new(params[:contact])  
@contact.customer = @customer

respond\_to do |format|  
if @contact.save  
flash[:notice] = ‘Contact was successfully created.’  
format.html { redirect\_to([@customer,@contact]) }

private

## def load\_parent @customer = Customer.find(params[:customer\_id]) end

My contacts/new.html.erb includes this change:

\<% form\_for([@customer,@contact]) do |f| %\>

* * *

So now, in customers/show.html.erb I add a link to new contact page:

# \<%= link\_to “Add new Contact”, new\_customer\_contact\_path(@customer) %\>

* * *

Everything is working correctly at this stage. Clicking the link takes  
me to the New Contact page (/customers/1/contacts/new) where I can  
create a new contact. The new contact gets properly saved to the DB  
including the correct id in the customer\_id column.

However, there are 2 problems:

1. After successfully creating the contact, I am presented with an  
“interim” page which says “Contact was Successfully created…You are  
being Redirected (link)”. No redirection occurs however. I have to  
click the link which takes me back to the customers page.

2. The other problem is that when I try to view all the contacts I’ve  
created for a specific customer, I’m presented with all the contacts for  
each customer.

So visiting /customers/2/contacts shows me all contacts for all  
customers (#1, #2, #3, etc.) when it should only display contacts with  
customer\_id = 2.

I can’t figure out where I’ve gone wrong? The DB records the proper  
relationships and the redirect in the create contact action seems to be  
right.

Also, half the tutorials I find online still use the following method  
for nested resources:  
map.resources :customers, :has\_many =\> :contacts

My understanding is that the only difference with this method is you  
don’t get as many named routes for free and that this is the pre-Rails  
2.0.0 way of nesting resources. Am I correct in this assumption or are  
there other differences?

Thanks for your suggestions. Any help would be most appreciated. I’m  
really at a loss for how to fix this issue.

Thanks!

---

<div class="post-metadata">

**Author:** ![emh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3bc359/32.png) [@emh](https://www.ruby-forum.com/u/emh)\
**Post date:** [May 21, 2008, 5:57am UTC](https://www.ruby-forum.com/t/nested-resource-you-are-being-redirected-interim-page/138154/2 "2008-05-21T05:57:44Z")

</div>

Sorry…made an error in noting my routes.rb file. it is actually:

routes.rb

map.resources :customers do |customerS|  
customerS.resources :contacts  
end
