I need help, this debugging has been on for weeks. This code runs well and create stripe id in development mode, but when i try deploying the app on Render, on clicking the button to take the form to the sign up page, it brings up an error message of “Missing Plans, Select a Valid Plan”
This is my registration controller; class Users::RegistrationsController < Devise::RegistrationsController before_action :validate_plan, only: [ :new, :create ] # Ensure we validate before creating the user
GET /users/sign_up
def new # Find the selected plan by ID from the parameters @plan = Plan.find(params[:plan])
unless @plan
flash[:alert] = "Invalid plan selected. Please choose a valid plan."
redirect_to root_path and return
end
@user = User.new
super # This will continue to render the registration form if plan is valid. Sets the resource available
end
POST /users
def create # Find the selected plan by ID from the parameters @plan = Plan.find(params[:plan])
# If plan is invalid, it was already caught by the before_action, so just return
unless @plan
flash[:alert] = "Invalid plan selected."
redirect_to root_path and return
end
# Create a new user with the provided user parameters
@user = User.new(user_params)
@user.plan_id = @plan.id # Associate the selected plan with the user
if @user.valid?
begin
# Create a Stripe customer using the email and stripeToken (payment method) passed in the request
customer = Stripe::Customer.create(
description: @user.email,
email: @user.email,
source: params[:stripeToken] # The Stripe token for the payment method
)
# Save the Stripe customer ID for future reference
@user.stripe_customer_token = customer.id
# Handle subscriptions based on the selected plan (Pro or Basic)
if @plan.id == 2 # Pro plan
subscription = Stripe::Subscription.create(
customer: customer.id,
items: [ { price: "price_1QdCqEE1sSOCEmuor86NKBoA" } ]
)
@user.stripe_subscription_id = subscription.id
elsif @plan.id == 1 # Basic plan
price_id_basic = "price_1QdCpiE1sSOCEmuo4RN4elGq"
subscription = Stripe::Subscription.create(
customer: customer.id,
items: [ { price: price_id_basic } ]
)
@user.stripe_subscription_id = subscription.id
end
# Save the user object to the database
@user.save!
# Automatically sign the user in after successful registration
sign_in(@user)
# Redirect the user to the homepage with a success notice
redirect_to root_path, notice: "Account created successfully!"
rescue Stripe::StripeError => e
@user.errors.add(:base, "Stripe error: #{e.message}")
render :new
rescue => e
@user.errors.add(:base, "There was an error processing your request: #{e.message}")
render :new
end
else
render :new
end