Not very common Rails error
I’m new in Ruby Development with Rails, but i find it pretty fun and easy to understand, in my journey i started to make my first web app, using some Gems like Devise for Auth and CanCanCan for permissions.
Everything was working perfectly, until it doesn’t. The client asks for some minor changes in the info needed, and immediately started to code the changes, the problem was an error, a pretty annoying error every time i sent my data via POST, this was the error:
ActiveModel::ForbiddenAttributesError
Well, the problem can look very simple and easy to understand, but i was not. As you can imagine, usually when you got this error, Rails tells you what attribute is missing and need to be attached to your allowed white list, but Rails wasn’t working that way, why?, i didn’t know, but this problem took 2 (very frustrating) days of my life, so searching for a answer, i find a very old error report on Github about the old version of CanCanCan, called CanCan, the error was the same as mine, but… lots of time (3 years) has passed since then, so it can’t be, right?. Well, fuck, it is the same problem.
Let me explain a little, the problem is, when the error happens, this CanCanCAn method: load_and_authorize_resource, doesn’t let rails show the real error behind. The load_and_authorize_resource method is used on your controllers to filter the users, according to CanCanCan Ability file, so is usually on every controller you use.
The Solution:
The solution is absurdly simple, just comment your method:
# load_and_authorize_resource
And now you can see what attribute you need to add to your white list.