Evan Hayes
2008-May-20 15:17 UTC
Nested Resource - "You are Being Redirected" interim page
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!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Evan Hayes
2008-May-21 03:57 UTC
Re: Nested Resource - "You are Being Redirected" interim pag
Sorry...made an error in noting my routes.rb file. it is actually:
routes.rb
map.resources :customers do |customerS|
customerS.resources :contacts
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---