【问题标题】:Unable to autoload constant Users, expected /app/serializers/users.rb to define it无法自动加载常量用户,需要 /app/serializers/users.rb 来定义它
【发布时间】:2019-01-28 04:13:00
【问题描述】:

当我尝试使用 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

【问题讨论】:

    标签: ruby-on-rails login serialization


    【解决方案1】:

    渲染集合时,需要指定序列化器。尝试将您的索引操作更改为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 2019-12-25
      • 1970-01-01
      相关资源
      最近更新 更多