# Redirecting to page from the model

**URL:** <https://www.ruby-forum.com/t/redirecting-to-page-from-the-model/130275>\
**Category:** Rails\
**Created:** [February 26, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/redirecting-to-page-from-the-model/130275 "2008-02-26T23:19:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [February 26, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/redirecting-to-page-from-the-model/130275/1 "2008-02-26T23:19:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [February 26, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/redirecting-to-page-from-the-model/130275/2 "2008-02-26T23:34:31Z")

</div>

That logic should go in a controller.

The model should perform can perform the validation and authorization  
but the result should be returned to the controller to execute the  
business logic.

---

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [February 26, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/redirecting-to-page-from-the-model/130275/3 "2008-02-26T23:32:11Z")

</div>

On Feb 26, 5:18 pm, Keaja [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts "The transaction was unsuccessful because
> 
> ```
> 
> end  
> glad i can help there buddy

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  
should never do logic in your model. NEVER

---

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [February 26, 2008, 11:37pm UTC](https://www.ruby-forum.com/t/redirecting-to-page-from-the-model/130275/4 "2008-02-26T23:37:28Z")

</div>

Thanks guys, alot of really useful information.

Everytime I think I am getting a handle on ruby, I realize how much I  
still have to learn haha =)

---

<div class="post-metadata">

**Author:** ![keaja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/838e76/32.png) [@keaja](https://www.ruby-forum.com/u/keaja)\
**Post date:** [February 26, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/redirecting-to-page-from-the-model/130275/5 "2008-02-26T23:35:07Z")

</div>

> glad i can help there buddy
> 
> 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
