You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Devise Omniauth报错:Users::OmniauthCallbacksController找不到github方法

Troubleshooting "The action 'github' could not be found for Users::OmniauthCallbacksController"

Let’s walk through the most likely issues that could be causing this error, even though you’ve already checked a lot of boxes:

1. Double-check your controller file path and naming

Rails relies heavily on convention, so your controller file must be at:
app/controllers/users/omniauth_callbacks_controller.rb
Make sure the users/ folder exists inside controllers/, and the filename is all lowercase with underscores (no camelCase or typos like omniauth_callback_controller.rb missing an "s").

2. Verify your controller class structure

Looking at your code snippet, ensure the github method is inside the controller class with proper indentation. A common mistake is accidentally placing the method outside the class definition, which means Rails won’t recognize it as part of the controller. Your controller should look like this:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def github
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication
      set_flash_message(:notice, :success, kind: "Github") if is_navigational_format?
    else
      redirect_to root_path
    end
  end
end

No missing end keywords, and the def github is properly nested under the class.

3. Restart your Rails server!

This is such a simple step but easy to overlook. Any changes to routes, controllers, or Devise initializers require a server restart to take effect. If you’ve been modifying code without restarting, the server is still running old code that doesn’t include your github method.

4. Confirm your route mapping

Run rails routes (or rake routes for older Rails versions) and look for the GitHub callback route. It should look like this:

user_github_omniauth_callback GET|POST /users/auth/github/callback(.:format) users/omniauth_callbacks#github

Double-check that the action is indeed github and the controller is users/omniauth_callbacks. If it’s pointing to a different action or controller, your devise_for route configuration might have a typo.

Even though the error points to the controller, a syntax error in your User model or Devise initializer could break the loading of related code. For example:

  • In your User model’s from_omniauth method, ensure all end keywords are present and properly nested. Also, first_or_create automatically saves the user, so you don’t need user.save! (this won’t cause the action-not-found error, but it’s a good cleanup).
  • Make sure your Devise initializer’s config.omniauth :github line is inside the Devise.setup do |config| block, with no typos in the provider name (:github, not :Github or :gitHub).

6. Rule out route conflicts

Check if any other routes in your routes.rb are defined before the devise_for :users line. A wildcard route or a similarly named route might be overriding the Omniauth callback route, preventing Rails from reaching your controller. Always place devise_for before custom routes to avoid this.

Start with the first three points—they’re the most common culprits for this exact error. If none of these work, try creating a minimal test case (a fresh Rails app with just Devise and Omniauth-GitHub) to see if the issue is specific to your app’s configuration.

内容的提问来源于stack exchange,提问作者Alli T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:41:49