Well, you could raise an error. That's how begin/rescue blocks work. It's not a good idea, though - using error handling for business logic is generally frowned upon.
It seems as though it would make much more sense to refactor as a simple conditional. Something like:
def function
@document = Invoice.find_by(:token => params[:id])
if @document.sent_at < 3.days.ago
flash[:error] = "Document has expired."
redirect_to root_path
else
@document.mark_as_viewed
end
end
It seems as though you've confused a few different kinds of block-related keywords here:
Error handling (begin
/rescue
/end
) is for cases where you think something you try might raise an error, and respond in a particular way to it.
next
is for iteration - when you're looping through a collection and want to skip to the next element.
Conditionals (if
, unless
, else
, etc.) are the usual way for checking the state of something and executing different bits of code depending on it.
rescue
correctly... – Customdef...rescue...end
as opposed todef...begin...rescue...end...end
– Stoddart