Nested Resource - "You are Being Redirected" interim page


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

My contact controller includes these changes:

class ContactsController < ApplicationController
before_filter :load_parent

def create
@contact =[:contact])
@contact.customer = @customer

respond_to do |format|
flash[:notice] = ‘Contact was successfully created.’
format.html { redirect_to([@customer,@contact]) }


def load_parent
@customer = Customer.find(params[:customer_id])

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

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.


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


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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs