pedalpete
pedalpete

Reputation: 21536

abort in rails loop function

I've got a function in my rails controller (I know, not the rails way, but I find it easier to write in the controller when I have something big like this and then move to the model).

I have an error in a array that I'm looping through, unfortunately, the error is being added somewhere in the loop. It is a big array with lots of properties, and I'm trying to figure out where the error is being caused.

I think I can isolate which object in the array is causing the error, but I can't get it to print.

Aparently ruby has an

abort('message')
function, but that returns an error in rails.

return render isn't working, it gives me an error that render and/or redirect is being called multiple times. How can I do a php type die in this situation?

Upvotes: 0

Views: 1289

Answers (1)

ScottJShea
ScottJShea

Reputation: 7111

This SO Post makes an excellent suggestion.

raise RuntimeError, 'Message goes here'

In the 'Message goes here' section you could even add in the array element:

array.each do |array_element|
     <logic>
     raise RuntimeError, "#{array_element.inspect}; Message goes here"
end

Upvotes: 1

Related Questions