#4 new
humancopy

[patch] fix for multi-nested resources

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

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]

Comments and changes to this ticket

  • humancopy

    humancopy April 28th, 2009 @ 10:17 PM

    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. :)

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.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

resources_controller is a rails plugin that eases the pain of RESTful controllers.

People watching this ticket

Attachments

Pages