Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

[patch] fix for multi-nested resources

#4

hey ...

this patch fixes 2 bugs i found: when trying to use multi-nested resources & when the nested resource uses has_one or bleongs_to.

for example trying to access something like this: /categories/1/threads/2/posts

would throw out 2 exceptions:

  1. method not found for @thread.categories in specification.rb line # 74 (fixed using a respond_to? check first)
  2. Ardes::ResourcesController::ResourceMismatch in resources_controller.rb line # 738 (fixed by adding .reverse to the generated enclosing names array)

thanks :)

routing


# accessed /categories
map.resources :categories do |cat|
    # accessed /categories/:category_id/threads
    cat.resources :threads do |thrd|
      # accessed /categories/:category_id/threads/:thread_id/posts
      thrd.resources :posts
    end
end

model associations

category.rb


has_many :threads

thread.rb


belongs_to :category
has_many :posts

post.rb


belongs_to :thread

controllers

categories_controller.rb


resources_controller_for :categories

threads_controller.rb


resources_controller_for :threads, :in => :category

posts_controller.rb


resources_controller_for :posts, :in => [:category, :threads]

Reported by humancopy · April 26th, 2009 @ 11:09 AM

State: new
Milestone: none
Assigned to: Ian White Ian White

Activity

  1. humancopy
    humancopy

    well, this quick patch didn't work all the way through (hash being a hash) :)

    then i found a better way, using recognized_route and it's significant_keys array, which is in the right order. if no recognized_route found it will revert to using params as before...

    changing line # 738 in resources_controller to:

    
    @route_enclosing_names = (recognized_route and recognized_route.significant_keys or params).select{|k| k.to_s =~ /_id$/}.map{|id| [id.to_s.sub('_id','').pluralize, false]}
    

    attached a patch. :)

    April 28th, 2009 @ 10:17 PM

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.