1
votes

I am sending an invitation to a new user using Devise Invitable. I have a related table to Users called Organizations, with a belongs_to/has_many relationship. When the new user goes to accept the invitation, it states that there must be an organization specified. How would I go about making sure the user is assigned to the inviting user's organization automatically, so that the database is updated and the invitation can be accepted?

Thank you!

Modeling after the invitation token as a hidden field, I added

<%= f.hidden_field :plan_id, :value => 1 %>
<%= f.hidden_field :organization_id, :value => 1 %>

to the edit.html.erb invitations view. This works to submit the information I'm looking for, but I'm worried that it is not secure, as those parameters could be adjusted. I also tried adding the following to the edit control in the invitations controller, but it did nothing.

resource.plan_id = 1
resource.organization_id = 1
2
You should list what you've already tried. See stackoverflow.com/help/how-to-askGab
I edited to add more detail.waffle

2 Answers

3
votes

I have a similar setup, have you tried something like this in the create section of your invitations controller?

This approach works for me, but you'll need to override the default invitations controller do it this way.

  def create
     @invited_user = User.invite!({:email => "[email protected]"}, current_user)
     @invited_user.update(organization: current_organization)
  ...
  end

I have current_organization defined in my application_controller.rb file as:

  def current_organization
    @current_organization ||= current_user.organization
  end
1
votes

if you use devise invitable, Overwrite your invitations controller's new and update actions. In the new action associate the new user to the group, in the update action permit the foreign key and you should be good to go.