无法自动加载常量用户,需要/app/serializers/users.rb进行定义

时间:2018-08-21 22:50:52

标签: ruby-on-rails login serializer

当我尝试使用facebook oauth登录时出现此错误。

Unable to autoload constant Users, expected /Users/cynthia/.atom/projects/Jquery_Portfolio_Project/tasks_with_JQuery/app/serializers/users.rb to define it

raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)

我认为序列化程序的名称正确,可能是缺少的属性吗?

class UserSerializer < ActiveModel::Serializer
  attributes :id, :email
  has_many :tasks
end


class UsersController < ApplicationController
  before_action :authenticate_user!

  def index
    @users = User.all
    respond_to do |format|
      format.html { render show: @users }
      format.json { render json: @users }
    end
  end

  def show
    @user = User.find_by(id: params[:id])
    respond_to do |format|
      format.html { render show: @user }
      format.json { render json: @user.tasks }
    end
  end
end

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

  has_many :tasks
  has_many :group_tasks, through: :tasks


  def self.new_with_session(params, session)
    super.tap do |user|
      if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
        user.email = data["email"] if user.email.blank?
      end
    end
  end

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
    end
  end
end

1 个答案:

答案 0 :(得分:0)

呈现集合时,您需要指定一个序列化器。尝试将索引操作更改为此:

  def index
    @users = User.all
    respond_to do |format|
      format.html { render show: @users }
      format.json { render json: @users, serializer: UserSerializer }
    end
  end