h1. Cancan, Inherited Resources, and Nested Routes
p(meta). 07 Oct 2010 - Balitmore, MD
h2. Problem:
My subscription does not have a subscriber.
InheritedResources is not populating the `belongs_to` item.
SubscriptionsController<<InheritedResources::Basebelongs_to:subscriberload_and_authorize_resourcedefcreate@subscription=build_resource# do something with subscriptioncreate!endend
In `def create`, `@subscription.subscriber` is nil. This *should* be populated by InheritedResources during #build_resource.
Unfortunately, Cancan's load_resource is populating the @subscription instance var, so build_resource simply returns the value of the instance var (get_resource_var).
h2. Solution:
Don't ask cancan to load the resource.
SubscriptionsController<<InheritedResources::Basebelongs_to:subscriberauthorize_resourcedefcreate@subscription=build_resource# do something with subscriptioncreate!endend
Or...
SubscriptionsController<<InheritedResources::Basebelongs_to:subscriberdefcreate@subscription=build_resourceauthorize!:create,Subscription# do something with subscriptioncreate!endend
Note: this appears to only be an issue if using nested routes.
resources:subscriberdoresources:subscription
This was identified on Rails 3. May affect Rails 2.