2012-07-04 23 views
8

Vì vậy, tôi đã ứng dụng này sử dụng omniauth-facebook để autenticate người dùng, người dùng mới tạo ra trong sessionscontroller:Lỗi định tuyến - UsersController không xác định không được khởi tạo? omniauth-facebook

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 

sau đó nó chạm vào mô hình người dùng:

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 

Sau đó, trong bộ điều khiển người dùng của tôi Tôi có thông tin như thế này để hiển thị tiểu sử của người dùng:

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

Và trong show.html.erb tôi có nội dung như sau:

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

Nhưng tôi nhận được lỗi sau: Routing Error - uninitialized constant UsersController

tuyến My 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 

Trả lời

20

Đó là một chút khó khăn để nói những gì các vấn đề ở đây là: trong tương lai, hãy xem xét đăng toàn bộ thông báo lỗi và mã mà thông báo lỗi tham chiếu là xấu.

Tuy nhiên, tôi đoán là: UsersController của bạn có tên là UserController. Nó nên được số nhiều. Thay đổi tên thành UsersController và điều này sẽ hoạt động.

+0

Đó là vấn đề, thực ra tôi là lỗi lỗ là: Lỗi định tuyến - hằng số không xác định UsersController - thử chạy các tuyến đường rake. – SHUMAcupcake

+1

cảm ơn! vừa giải quyết cùng một vấn đề cho tôi! – dopplesoldner

+3

Lưu ý rằng 'user' trong tên tập tin cũng cần phải được số nhiều:' users_controller.rb'. – Dennis

Các vấn đề liên quan