Hi, can anyone help with a routing problem for nested resources: My models are User (has_one :house) and House (belongs_to :user). I have a houses_controller and a user_house_controller. house_controller is for the admin to perform CRUD on houses; so users'' CRUD must go through the user_house_controller. The resources are nested like so: map.resources :houses map.resources :users do |users| users.resource :house, :controller => ''user_house'' end So after a new user registers I want to send him to the form for the his house, ie http://localhost:3000/users/21/gym/new I''m having trouble with generating this URL programmatically. In my users_controller I have: def create @user = User.new(params[:user]) #user registered, now register his house redirect_to new_gym_url(:user_id => @user) I know when one has a nested resource then the URL generator takes 2 arguments: The nested-under model and the nested model. So according to this logic, the URL helper should look like: new_gym_url(:user_id => @user, :id => house) But then I will be referring to a house that hasn''t been created yet! When I just use: new_gym_url(:user_id => @user) or new_gym_url I get redirected to the houses_controller#new, and I want to use user_house_controller. This is about to drive me nuts; so please explain! / Vahagn -- 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 -~----------~----~----~----~------~----~------~--~---
PS! Correct url is http://localhost:3000/users/21/house/new :-) -- 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 -~----------~----~----~----~------~----~------~--~---
Issue solved! users.resource :houses, :controller => ''user_house'' instead of: users.resource :house, :controller => ''user_house'' end Apparently the resources are always plural, regardless that a user only has one house. /V. -- 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 -~----------~----~----~----~------~----~------~--~---
On Nov 4, 8:08 am, Vahagn Hayrapetyan <rails-mailing-l...@andreas- s.net> wrote:> Issue solved! > > users.resource:houses, :controller => ''user_house'' > > instead of: > > users.resource:house, :controller => ''user_house'' > end > > Apparently the resources are always plural, regardless that a user only > has one house. > > /V.Hi Vahagn. Resources don''t have to be plural. For example, one of my apps has users, and when they login, they access their account at this path: /account/ To do this, I added the following to routes.rb: map.resource :account You just need to make sure that all of your files and classes have proper pluralisation. Thus, I have AccountsController. For your question, I would think that you''d need: # config/routes.rb map.resources :users do |users| users.resource :house, :controller => ''user_house'' end # app/controllers/houses_controller.rb class HousesController < ApplicationController In other words, make sure it''s "HousesController" and "houses_controller.rb" (plural), not "HouseController" or "house_controller.rb" (singular). Once you have that working properly, you can generate URLs and paths to a user''s house like this: user_house_path user_house_url I hope that helps, Nick --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Nick,- thanks for your comment, I appreciate it. I''ve worked around the problem but it is definitely helpful. Previously I thought that pluralization patterns in the routes file must follow those of the controllers but now I can see it''s not the case - the two things are decoupled and the most important thing is the proper pluralization of the controllers. All the best, Vahagn Nick Hoffman wrote:> On Nov 4, 8:08�am, Vahagn Hayrapetyan <rails-mailing-l...@andreas- > s.net> wrote: >> has one house. >> >> /V. > > Hi Vahagn. Resources don''t have to be plural. For example, one of my > apps has users, and when they login, they access their account at this > path: > /account/ > To do this, I added the following to routes.rb: > map.resource :account > You just need to make sure that all of your files and classes have > proper pluralisation. Thus, I have AccountsController. > > For your question, I would think that you''d need: > # config/routes.rb > map.resources :users do |users| > users.resource :house, :controller => ''user_house'' > end > > # app/controllers/houses_controller.rb > class HousesController < ApplicationController > > In other words, make sure it''s "HousesController" and > "houses_controller.rb" (plural), not "HouseController" or > "house_controller.rb" (singular). > > Once you have that working properly, you can generate URLs and paths > to a user''s house like this: > user_house_path > user_house_url > > I hope that helps, > Nick-- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---