Nesting devise_for :users within a parent resource

749 views
Skip to first unread message

Ryan Daigle

unread,
Nov 20, 2010, 10:40:57 PM11/20/10
to Devise
I'm trying to scope user accounts to businesses (much like the more
common example of tying users to subdomains). I'd like my routes to
look something like:

/businesses/1/users/sign_in

The following routes definition:

resources :businesses do
devise_for :user
end

results in the somewhat inversed routes definition (notice :users is
the top level resource - I'm looking for :users to be nested
under :businesses):

/user/businesses/1/sign_in

I've tried a couple of different variations with the only thing coming
close to working being something like so:

resources :businesses do
as :user do
resource :users, :controller => "devise/sessions" do
get 'sign_in', :to => "devise/sessions#new"
end
end
end

but this has a lot of side effects that tell me this isn't quite the
right approach (like various helpers and default routes not being
accurate).

Is there a devise-sanctioned approach to accomplishing the nesting of
user routes within a parent resource?

Ryan Daigle

unread,
Nov 22, 2010, 3:54:54 PM11/22/10
to Devise
I'm definitely running in circles on this one. I'm close to just
subclassing all of Devise's controllers in my own app and manually
defining each route. This feels completely wrong, though, and I can't
help but feel that I'm missing something pretty straight-forward.

To re-state my issue, I want to get devise users, and all their
routing goodness, exposed as a nested resource of a "business"
resource so my routes look something like:

/businesses/:biz_id/users/sign_in

Is this possible without resorting to custom routes/controllers/
helpers?

Thanks!

-Ryan

Carlos Antonio da Silva

unread,
Nov 22, 2010, 5:39:09 PM11/22/10
to plataforma...@googlegroups.com
Devise controllers won't know about your nested resource, which means it won't know how to filter the user based on the current business coming from the url, you'd have to do this manually.

Now about the routes, I really don't get how they're being generated this way.
If you can provide an app showing your issue, would help.
--
At.
Carlos A. da Silva

Ryan Daigle

unread,
Nov 23, 2010, 8:57:41 AM11/23/10
to plataforma...@googlegroups.com
What I'm settling on for the time being, and it seems to be going ok so far, is to wrap my devise_for within a scope that mimics being a nested resource:

resources :businesses
scope "businesses/:business_id" do
  devise_for :users
end

Then all I have to do is update the sessions/_form partial to include the business_id from params into the form as a hidden field. All the other route helpers etc... seem to be working with this setup, properly including the business_id when necessary to build a path.

I'll update this if there are any more tricks or updates needed to get this type of solution working.

Are there any plans to make Devise more resource-aware? Right now it feels a little bit like a bad citizen in the routing world since it doesn't play well with others. Just a thought.

-Ryan

Carlos Antonio da Silva

unread,
Nov 23, 2010, 10:27:50 AM11/23/10
to plataforma...@googlegroups.com
Yeah, this way should work pretty well actually.
We'll have to look at what might be causing this to happen, but it'd be really awesome if you could take a look, maybe fix and send us a pull request if you find sth wrong.

Thanks.

bangarang3

unread,
Nov 24, 2010, 12:16:44 PM11/24/10
to Devise
awesome, thanks for this.... I actually had the EXACT same issue and
had been banging my head against the wall for days. i was about to
give up and just subclass everything and re-write the routes from
scratch

On Nov 23, 8:57 am, Ryan Daigle <ryan.dai...@gmail.com> wrote:
> What I'm settling on for the time being, and it seems to be going ok so far, is to wrap my devise_for within a scope that mimics being a nested resource:
>
> resources :businesses
> scope "businesses/:business_id" do
>   devise_for :users
> end
>
> Then all I have to do is update the sessions/_form partial to include the business_id from params into the form as a hidden field. All the other route helpers etc... seem to be working with this setup, properly including the business_id when necessary to build a path.
>
> I'll update this if there are any more tricks or updates needed to get this type of solution working.
>
> Are there any plans to make Devise more resource-aware? Right now it feels a little bit like a bad citizen in the routing world since it doesn't play well with others. Just a thought.
>
> -Ryan
>
> On Nov 22, 2010, at 5:39 PM, Carlos Antonio da Silva wrote:
>
>
>
>
>
>
>
> > Devise controllers won't know about your nested resource, which means it won't know how to filter the user based on the current business coming from the url, you'd have to do this manually.
>
> > Now about the routes, I really don't get how they're being generated this way.
> > If you can provide an app showing your issue, would help.
>

Mark Richman

unread,
Jan 1, 2011, 3:23:45 PM1/1/11
to plataforma...@googlegroups.com
Ryan,

Would you mind posting your sessions/_form partial? I'm trying to do the same thing.

Thanks,
Mark
Reply all
Reply to author
Forward
0 new messages