Ruby-on-rails 路由错误 - 未初始化的常量 UsersController?omn​​iauth-facebook

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11331391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:38:38  来源:igfitidea点击:

Routing Error - uninitialized constant UsersController? omniauth-facebook

ruby-on-railsrubyruby-on-rails-3

提问by SHUMAcupcake

So I have this app that uses omniauth-facebook to autenticate users, a new user creates in the sessionscontroller:

所以我有一个使用 omniauth-facebook 来验证用户的应用程序,一个新用户在 sessioncontroller 中创建:

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env['omniauth.auth'])
    session[:user_id] = user.id
    redirect_to root_url, notice: "Signed in!"
  end
end

then it hits the user model:

然后它击中用户模型:

class User < ActiveRecord::Base
  def self.from_omniauth(auth) 
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"] unless auth["info"].blank?
      user.email = auth["info"]["email"] unless auth["info"].blank?
      user.save!
    end
  end
end

Then in my usercontroller I have something like this to display a user profil:

然后在我的用户控制器中,我有这样的东西来显示用户配置文件:

class UserController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end

And in the show.html.erb I have something like this:

在 show.html.erb 我有这样的东西:

<%= @user.name %>, <%= @user.email %>

But I get the following error: Routing Error - uninitialized constant UsersController

但我收到以下错误: Routing Error - uninitialized constant UsersController

My routes file:

我的路线文件:

Bummerang::Application.routes.draw do


    resources :users, :only => :show

    root to: 'static_pages#home'

    match '/about',   to: 'static_pages#about'
    match '/contact', to: 'static_pages#contact'

    # autentications routes
    match '/auth/:provider/callback', to: 'sessions#create'
    match 'signout', to: 'sessions#destroy', as: 'signout'
    match 'auth/failure', to: redirect('/')
end

回答by Veraticus

It's a little difficult to tell what the problem here is: in the future, consider posting the entire error message and the code that the error message references as being bad.

很难说出这里的问题是什么:将来,请考虑发布整个错误消息以及错误消息引用的错误代码。

Nevertheless, my guess is: your UsersControlleris named UserController. It should be pluralized. Change the name to UsersControllerand this should work.

尽管如此,我的猜测是:您UsersController的名字是UserController. 它应该是复数形式。将名称更改为UsersController,这应该可以工作。