How should I handle redirecting to pages from the model?
Basically I have credit cards that get processed in a model. If
successful, I want the user to be redirected to a certain page, of
unsuccessful I would like them redirected to a new page.
Obviously if I try and do this in the model, I get a undefined method
`redirect_to' error. How should I handle this?
a method in my model:
def authorize_payment
if creditcard.valid?
response = gateway.authorize(100, creditcard, options)
print "(TEST) " if response.test?
if response.success?
puts "The transaction was successful! The authorization is
#{response.authorization}"
redirect_to :controller => '/account', :action => 'index'
flash[:notice] = "Thanks for signing up!"
else
puts "The transaction was unsuccessful because
#{response.message}"
redirect_to :controller => '/account/', :action =>
'signup_unlimited'
flash[:notice] = "There was a problem with your credit card,
please verify your information and try again."
end
else
puts "The credit card is invalid"
end
end
end
The model should perform can perform the validation and authorization
but the result should be returned to the controller to execute the
business logic.
if authorize_payment
redirect_to :action => "x"
else
redirect_to :action => "y"
end
You see, the model will throw false if it is not successful. You
sould never do logic in your model. NEVER
Also from my experience, not sure if it can be done another way, i
would declare the model function as
self.authorze_payment
that way, if the model is called, "payment", from the controller you
can call payment.authorize_payment. Inject that into the if statement
i gave you earlier and voila, perfect execution