Created
October 21, 2014 22:14
-
-
Save jah2488/55044621d1bf742239e5 to your computer and use it in GitHub Desktop.
It works! Kinda. This is some terrifying code, but it will give you method suggestions on the error you received in your rails code using the rails inflector.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::Base | |
| # Prevent CSRF attacks by raising an exception. | |
| # For APIs, you may want to use :null_session instead. | |
| protect_from_forgery with: :exception | |
| rescue_from NoMethodError, with: :try_to_inflect | |
| def try_to_inflect(err) | |
| error_class = err.class | |
| method_name = err.message.scan(/`(.*)'/).first.first | |
| method_pieces = method_name.split('_') | |
| method_suggestions = map_map(method_pieces) do |arr, item, index| | |
| myarr = arr.dup | |
| if item.pluralize == item | |
| myarr[index] = item.singularize | |
| else | |
| myarr[index] = item.pluralize | |
| end | |
| myarr | |
| end.map { |i| i.join('_') } | |
| new_error = error_class.new("Cannot find '#{method_name}' Perhaps you meant? \n #{method_suggestions}") | |
| new_error.set_backtrace(err.backtrace) | |
| raise new_error | |
| end | |
| private | |
| def map_map(arr, &block) | |
| arrs = [] | |
| arr.each_with_index do |item, index| | |
| arrs.push(block.call(arr, item, index)) | |
| end | |
| arrs | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment