Reputation: 15
I have been through a number of similar questions but still can't spot the multiple redirects in my action. Can you help me spot it? Also are there any generic tips for avoiding the so called Double Render Error?
The code is as follows:
def st_buy_now_process
#create user in system
encrypted = BCrypt::Password.create(params[:password])
if (User.where("email = ?", params[:email]).exists?)
flash[:alert] = 'User exists. Please try again.'
redirect_to "/st_buy_now"
elsif (User.create(email: params[:email], encrypted_password: encrypted))
begin
customer = Stripe::Customer.create({
:description => params[:email],
:card => {
:number => params[:cc],
:exp_month => params[:exp_1],
:exp_year => params[:exp_2],
:cvc => params[:cvc],
:name => params[:name]
},
:email => params['email']
})
p="#{params['plan']}_#{params['billing-cycle']}"
subscr = customer.subscriptions.create(:plan => p)
user = User.where("email = ?", params[:email]).first
user.payg = ({:cus_id => customer.id, :subscr_id => subscr.id}).to_json
user.plan = p
user.save
rescue Stripe::CardError => e
flash[:alert] = 'Please try again'
redirect_to "/st_buy_now"
end
RestClient.post "https://api:key-nnnn",
:from => "<[email protected]>",
:to => "#{params[:email]}",
:subject => "Welcome to test",
:text => "\n\n\n#{File.read("app/views/misc/mail_sign_up.txt")}\n\n\n"
cookies['logged_in'] = {
:value => '1',
}
cookies['us_id'] = {
:value => params[:email],
}
flash[:notice] = 'Registration successful.'
redirect_to :controller => 'misc', :action => 'create_1'
else
flash[:alert] = 'Please try again'
redirect_to "/st_buy_now"
end
end
The error message highlights the last but one redirect - the line after the "Registration successful."
alert.
Upvotes: 0
Views: 87
Reputation: 2575
What about this, if customer got created proceed further and redirect and if error occur redirect_to "/st_buy_now"
def st_buy_now_process
#create user in system
encrypted = BCrypt::Password.create(params[:password])
if (User.where("email = ?", params[:email]).exists?)
flash[:alert] = 'User exists. Please try again.'
redirect_to "/st_buy_now"
elsif (User.create(email: params[:email], encrypted_password: encrypted))
begin
customer = Stripe::Customer.create({:description => params[:email],:card => {:number => params[:cc], :exp_month => params[:exp_1],:exp_year => params[:exp_2], :cvc => params[:cvc], :name => params[:name]},:email => params['email'] })
p="#{params['plan']}_#{params['billing-cycle']}"
subscr = customer.subscriptions.create(:plan => p)
user = User.where("email = ?", params[:email]).first
user.payg = ({:cus_id => customer.id, :subscr_id => subscr.id}).to_json
user.plan = p
user.save
RestClient.post "https://api:key-nnnn",
:from => "<[email protected]>",
:to => "#{params[:email]}",
:subject => "Welcome to test",
:text => "\n\n\n#{File.read("app/views/misc/mail_sign_up.txt")}\n\n\n"
cookies['logged_in'] = {
:value => '1',
}
cookies['us_id'] = {
:value => params[:email],
}
flash[:notice] = 'Registration successful.'
redirect_to :controller => 'misc', :action => 'create_1'
rescue Stripe::CardError => e
flash[:alert] = 'Please try again'
redirect_to "/st_buy_now"
end
else
flash[:alert] = 'Please try again'
redirect_to "/st_buy_now"
end
end
Upvotes: 1
Reputation: 27971
When you rescue an exception, execution of your code continues on from there as normal, so your redirect_to "/st_buy_now"
in your rescue
block plus the redirect_to :controller => 'misc', :action => 'create_1'
you're getting the error on are your two redirects.
You probably want to throw a return
in that rescue
block.
Upvotes: 1