Does rails have a break statement?
I’m writing a controller that has some pretty complicated IF statements. In Sum what I’d like to do is something like this:
IF !All these variable exist and are not nil?
BREAK and don’t continue bec something went wrong
END
If XXX = ‘adsasd
do all this stuff
ELSE IF
ELSE
send out an error email
Is a break possible?
I don’t know when all of your variables become available, but when I want to do checks in the controller, I usually use the before_filter callbacks to do that. For example:
What this does is that when a request comes to action show in YourController, it will first call the private method check_if_variables_exist. If @your_variable is nil than it will end up at render :nothing => true and the filter_chain will be halted and the action show will never be called. If however your_variable is not nil, then the method will end without doing anything and the controller will then call the action show like usual.
So all the things you want to check beforehand can be placed in different before_filter callbacks. And if you don’t want the check for all actions in the Controller, it can be specified like this:
In my opinion, this is the “rails” way to do it.