# Redirect Loop

**URL:** <https://www.ruby-forum.com/t/redirect-loop/149901>\
**Category:** Rails\
**Created:** [September 30, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/redirect-loop/149901 "2008-09-30T23:20:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![croceldon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8491ac/32.png) [@croceldon](https://www.ruby-forum.com/u/croceldon)\
**Post date:** [September 30, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/redirect-loop/149901/1 "2008-09-30T23:20:51Z")

</div>

I have the following code in my controller:

def list\_by\_so\_number  
if request.get?  
@histories = History.find(:all, :conditions =\> [‘so\_number = ?’,  
params[:sales\_order\_number]], :order =\> ‘timestamp desc’)  
if @histories.length \< 1 then  
flash[:notice] = ‘There isn’t a Sales Order that matches the  
number you entered’  
redirect\_to :action =\> ‘list\_by\_so\_number’  
end  
end  
end

My problem is that when I purposely enter a bogus so\_number, I wind up  
in a redirect loop, because of the redirect\_to :action =\>  
‘list\_by\_so\_number’ line.

How can I rewrite this so that when no histories are found, it’ll  
redirect to the list\_by\_so\_number action, and notify the user?

Thanks,
