Devise Omniauth报错:Users::OmniauthCallbacksController找不到github方法
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.
5. Check for syntax errors in related files
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_omniauthmethod, ensure allendkeywords are present and properly nested. Also,first_or_createautomatically saves the user, so you don’t needuser.save!(this won’t cause the action-not-found error, but it’s a good cleanup). - Make sure your Devise initializer’s
config.omniauth :githubline is inside theDevise.setup do |config|block, with no typos in the provider name (:github, not:Githubor: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

