Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Undefined method error when using different invite use cases #832

Open
mikesb21 opened this issue Oct 6, 2020 · 5 comments
Open

Comments

@mikesb21
Copy link

mikesb21 commented Oct 6, 2020

I've followed this article from the wiki(Customizing for different Invite use cases). I did setup everything exactly as the documentation says, but when I get to call the invite_guest! method I get "undefined method 'invite_guest!' for #User:0x00007f11c2cdec60". The same happens for 'invite_friend!'

I am creating the user prior to sending the email, so when I call the method I do user.invite_guest!, the user already has the email and all its information, I have verified this in the console several times. By doing user.invite! everything works fine, the problem is only with the added methods(invite_guest! and invite_friend!)

If I do User.invite_guest!(email: "[email protected]") in the console, the email is sent and the error does not show. The error only happens when doing the mentioned above.

Any ideas on why this is happening? Am I doing something wrong?

@scambra
Copy link
Owner

scambra commented Oct 10, 2020

Can you post the line or method where you get the error?

@mikesb21
Copy link
Author

mikesb21 commented Oct 13, 2020

Sure, in my implementation the methods are called "invite_new_user!" and "invite_existing_user!". I did change the name of the methods in the model and everything else that was required such as the mail templates, etc.

def create_new_team_user(user, role, is_new_user)

    new_team_user = TeamUser.new
    new_team_user.id = user.id
    new_team_user.role = role

    if new_team_user.save
      if is_new_user
        user.invite_new_user!   # Conflicting line. As mentioned before, if we do "user.invite!" it works fine
      else
        user.invite_existing_user!  # Conflicting line
      end
    else
      puts new_user_program_team.errors.full_messages
    end
  end

Thanks in advance!

@scambra
Copy link
Owner

scambra commented Oct 14, 2020

You have created class methods and try to call instance methods. I don't know if you have added methods to TeamUser or User, I'm guessing you added them to user, but those methods in wiki expect email attribute, so I don't know if they are suitable for your use case.

Probably you can use instance invite! method after setting user.invitation_instructions:

  def create_new_team_user(user, role, is_new_user)
    new_team_user = TeamUser.new
    new_team_user.id = user.id
    new_team_user.role = role

    if new_team_user.save
      if is_new_user
        user.invitation_instructions = :new_user_invitation_instructions
      else
        user.invitation_instructions = :existing_user_invitation_instructions
      end
      user.invite!
    else
      puts new_user_program_team.errors.full_messages
    end
  end

I'm not sure if it would work for existing users, as they can keep only one invitation token, so inviting them to another team user would break previous invitation. Also, you could add devise to TeamUser, so you can call new_team_user.invite! for existing users and user.invite! for new users. For example:

  def create_new_team_user(user, role, is_new_user)
    new_team_user = TeamUser.new
    new_team_user.id = user.id
    new_team_user.role = role

    if new_team_user.save
      if is_new_user
        user.invite!
      else
        new_team_user.invite!
      end
    else
      puts new_user_program_team.errors.full_messages
    end
  end

@mikesb21
Copy link
Author

I used the first example provided but unfortunately, it didn't work. The email was sent which is good but with the default invite template not with new_user_invitation_instructions or existing_user_invitation_instructions.

For this last example, "user" is the object that has the email, new_team_user is only the link between a group and a user so sending emails to this is not possible.

@scambra
Copy link
Owner

scambra commented Oct 20, 2020

For last example, you could add an email method to TeamUser which returns user.email

If you want to use first example, can you add some debug info to your mailer class, so you can check if invitation_instructions is set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants