2

Recently I update my app from Ruby version 2.6.1 to 3.0.1 & I'm using rbenv as a version manager.

but when I try to run the rails server I got an error

=> Booting Puma
=> Rails 6.1.3 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/home/humayun/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/activesupport-6.1.3/lib/active_support/rescuable.rb:56:in `rescue_from': Need a handler. Pass the with: keyword argument or provide a block. (ArgumentError)
    from /home/humayun/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/will_paginate-3.1.8/lib/will_paginate/railtie.rb:67:in `rescue_from'
    from /home/humayun/umerfarooq/Alchemy/app/controllers/application_controller.rb:2:in `<class:ApplicationController>'
    from /home/humayun/umerfarooq/Alchemy/app/controllers/application_controller.rb:1:in `<main>'

I just read Here about the function which is causing an error on line 56.

applciation_controller.rb

 rescue_from Exception, with: :handle_exception
 protect_from_forgery prepend: true, with: :exception
 before_action :configure_permitted_parameters, if: :devise_controller?
 before_action :initialize_api

  def not_found
    raise ActionController::RoutingError.new('Not Found')
  end

 def handle_exception(exception = nil)
    return render_404 if [ActionController::RoutingError, ActiveRecord::RecordNotFound].include?(exception.class)
     render_500
 end

I think this is because of depreciation.

can anyone please tell me how to handle these errors?

Humayun Naseer
  • 440
  • 7
  • 18

1 Answers1

5

your handle_exception probably will need a block that render a view or returns a status

as mentioned in the error in app/controllers/application_controller.rb:2 you probably have a rescue_from without an error or a handler you need to follow any of below syntax

class ApplicationController < ActionController::Base
  rescue_from User::NotAuthorized, with: :deny_access # self defined exception
  rescue_from ActiveRecord::RecordInvalid, with: :show_errors

  rescue_from 'MyAppError::Base' do |exception|
    render xml: exception, status: 500
  end

  private
    def deny_access
      ...
    end

    def show_errors(exception)
      exception.record.new_record? ? ...
    end
end

as per documentation here https://apidock.com/rails/ActiveSupport/Rescuable/ClassMethods/rescue_from

------- Update:

This is due to the will_paginate gem that you are using that overrides the rescue_from method in your controller alongside the new ruby updates that change the behavior around the keyword attribute

if your base controller include ControllerRescuePatch you might be able to remove it and this will fix it but not sure what will happen to your pagination. Otherwise, hold off the ruby update until the will_paginate update their code to fix this

Mshka
  • 1,798
  • 1
  • 10
  • 19
  • i just update my question with application_controller.rb please can you help me little more , because I'm working on client project and i dnt know well about this – Humayun Naseer Apr 08 '21 at 09:09
  • @HumayunNaseer I updated with a possible fix, can you please try now? – Mshka Apr 08 '21 at 09:31
  • i just replace my line with your three lines with the methods below but the errors remains same :( – Humayun Naseer Apr 08 '21 at 10:12
  • I think this might be a bug related to https://rubyreferences.github.io/rubychanges/3.0.html#keyword-arguments-are-now-fully-separated-from-positional-arguments what if you remove the with and just use a block directly with the content of the methods - let me update the answer – Mshka Apr 08 '21 at 10:24
  • updated, let me know if it works, if not I'm afraid this might be a bug in rails – Mshka Apr 08 '21 at 10:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230885/discussion-between-mark-jad-and-humayun-naseer). – Mshka Apr 08 '21 at 10:33
  • i replied in discussion please have a look – Humayun Naseer Apr 08 '21 at 11:09