0

am using stripe for users to subscribe. from there i collected the stripe date and save it in subscription mode. i created enum for my user model to assign different roles base on the stripe subscription id. here is how my user model looks like

class User < ApplicationRecord
  enum role: [:user, :gold, :platinum, :diamond ]
  has_one :subscription
  has_one :shipping
  extend FriendlyId
 friendly_id :firstname, use: [:slugged, :finders]

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

end

and bellow is my subscription controller

   def subscription_checkout
     user = current_user
     plan_id = params[:plan_id]
     plan = Stripe::Plan.retrieve(plan_id)
     # This should be created on signup.
     customer = Stripe::Customer.create(
         description: 'Customer for [email protected]',
         source: params[:stripeToken],
         email: '[email protected]'
     )
     # Save this in your DB and associate with the user;s email
     stripe_subscription = customer.subscriptions.create(plan: plan.id)
     @sub = Subscription.create(plan: stripe_subscription.plan.name,
                                stripeid: stripe_subscription.id,
                                user_id: current_user.id,
                                customer: stripe_subscription.customer,
                                subscriptionenddate: stripe_subscription.current_period_end)
     if @sub.save
         current_user.role  plan.id
         current_user.save
         flash[:success] = 'sub created!'
         redirect_to root_url
     else
         render 'new'
     end
 end

when it reach to update the role i get

ArgumentError: wrong number of arguments (1 for 0)

how can i update the role and what am i doing wrong?

1 Answer 1

1

Have you tried: current_user.role = plan.id ?

It looks like you are just calling the role() method on the curent_user object.

Sign up to request clarification or add additional context in comments.

2 Comments

that works. now am getting undefined method diamond?' for #<User` even thou in my rails console returns user.diamond? => true
Where's your surrounding code? Did you hop into the console by placing a debugger before the error was thrown or did you just open IRB?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.