Daniel May
Daniel May

Reputation: 2262

How to nest devise_invitable route to invite user to specific project

Problem

I'm using devise and devise_invitable gems on a project management web app. I am successfully inviting new users to to the web app.

But what I need is to invite new users to a specific project and can't figure out how to do that. I need the project ID as part of the route so I can properly wire up the controller code.

Context

Relevant routes.rb section looks like:

devise_for :users, :controllers => { :invitations => "invitations" }

So:

(I think) I am looking to make something like this work:

users/invitation/projects/4f3423d34323/new

Upvotes: 5

Views: 1359

Answers (1)

Daniel May
Daniel May

Reputation: 2262

Current approach is to specify the route using devise_scope:

devise_for :users, :controllers => { :invitations => "invitations" }

devise_scope :user do 
   match "/projects/:project_id/invitations/new", :to => "invitations#new", :via => "get", :as => "new_project_invitation"
end  

This means I can use:

Case 1: users/invitation/new (for inviting new user to web app)

and

Case 2: projects/:project_id/invitations/new (for inviting new user to web app + project)

invitations_controller#new checks for presence of :project_id and invokes appropriate behaviour for Case 1 or 2.

Upvotes: 5

Related Questions