RailsApps / rails-stripe-membership-saas

An example Rails 4.2 app with Stripe and the Payola gem for a membership or subscription site.

Home Page:http://railsapps.github.io/rails-stripe-membership-saas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Devise Invitable and error when creating an account

albertpak opened this issue · comments

I'm using An invitation strategy for Devise and I get an error

undefined method `name' for nil:NilClass

and it references to this part of User model:

...
if !stripe_token.present? && roles.first.name != 'freetrial'
 raise "Stripe token not present. Can't create account."
end
...

Any suggestions of how to get around that?

You need to give that user a 'role' upon creation.

Yeah, i've tried doing that, i've created an invitations controller, following guide in Devise Invitable:

class Users::InvitationsController < Devise::InvitationsController

before_create :assign_role_on_invite

  def assign_role_on_invite
    self.add_role(:pro)
  end

end

but it's still throwing that error

self in that case is Users::InvitationsController.. You need to find the user and set add_role(:pro) on it, not on the controller class.

So I would need to see the rest of your code for the best recommendation but your looking to do something like:

class Users::InvitationsController < Devise::InvitationsController

before_create :assign_role_on_invite

  def assign_role_on_invite
    user = User.find(params[:id])
    user.add_role(:pro)
  end

end

unfortunately this didn't work either

the code for this project is actually hosted on bitbucket - and if it's possible - i'd be glad to invite you to that project for you to take a look at it - if that's doable at all.

Where are you creating the user at? In the create method I'm assuming? If so, post that method on here.

I have registrations_controller.rb:

class RegistrationsController < Devise::RegistrationsController

    def new
        @plan = params[:plan]
        if @plan && ENV["ROLES"].include?(@plan) && @plan != "admin"
            super
        else
            redirect_to root_path, :notice => 'Please select a subscription plan below.'
        end
    end

    def update_plan
        @user = current_user
        role = Role.find(params[:user][:role_ids]) unless params[:user][:role_ids].nil?
        if @user.update_plan(role)
            redirect_to edit_user_registration_path, :notice => 'Updated plan.'
        else
            flash.alert = 'Unable to update plan.'
            render :edit
        end
    end

    def update_card
        @user = current_user
        @user.stripe_token = params[:user][:stripe_token]
        @user.last_4_digits = params[:user][:last_4_digits]
        if @user.save
            redirect_to edit_user_registration_path, :notice => 'Updated card.'
        else
            flash.alert = 'Unable to update card.'
            render :edit
        end
    end

    private
    def build_resource(*args)
        super
        if params[:plan]
            resource.add_role(params[:plan])
        end
    end
end

and I thought that this is where it adds the role to resource. So then I thought I'd follow the guide on Devise Invitable ( Configuring controllers ), I created my own controller that inherits from Devise Invitable controller and add my own action...

class Users::InvitationsController < Devise::InvitationsController

    before_create :assign_role_on_invite

    private
        def assign_role_on_invite
           resource.add_role(:pro)
        end

end

Thank you in advance :)

I tried adding

<%= hidden_field_tag 'plan', "pro" %>

to the form that appears /devise/invitations/new.html.erb. Inspecting HTML, and I do see that field generated:

<input id="plan" name="plan" type="hidden" value="pro">

but it's still throwing same error.

I've tried code snippets that were provided in #390

So the error that I'm getting is:

NoMethodError at /users/invitation
undefined method `name' for nil:NilClass

rails_app2013

In Request Info section, I see that the controller is "controller"=>"devise/invitations"

routes.rb

...
    devise_for :users, :controllers => { :registrations => 'registrations' }
    devise_for :users, :controllers => { :invitations => 'invitations' }
...

and this is my invitations_controller.rb

class Users::InvitationsController < Devise::InvitationsController
    private
        def invite_resource
            resource_class.invite!(invite_params, current_inviter) do |invitable|
                invitable.add_role(:pro)
            end
        end
end

Should't it be using the controller that I've created?