【问题标题】:Ruby on rails LoadErrorRuby on rails LoadError
【发布时间】:2013-10-23 14:07:45
【问题描述】:

我正在尝试使用以下行来调试我的日志:

<%= debug $pvp_user.pvp_death_logs %>

我收到的错误:

无法自动加载常量 PvpDeathLog,需要 /var/www/rails/ourapp/app/models/pvp_death_log.rb 来定义它

models/pvp_user.rb

class Pvp_user < ActiveRecord::Base
    has_many :pvp_death_logs
end

models/pvp_death_log.rb

class Pvp_death_log < ActiveRecord::Base
    belongs_to :pvp_user
end

controllers/player_controller.rb

class PlayerController < ApplicationController
  def index
  end

  def show
    user = Mc_user.find_by username: params[:id]
    if !user
        flash[:status] = FALSE
        flash[:alert] = 'we couldn\'t find that user.'
    else
        pvp_user = Pvp_user.find_by username: params[:id]
        if !pvp_user
            flash[:status_pvp] = FALSE
            flash[:alert_pvp] = 'No player vs player profile found.'
        else
            flash[:status_pvp] = TRUE
            $pvp_user = pvp_user
        end

        flash[:status] = TRUE
        $user = user
    end
  end
end

views/player/show.html.erb

<%= debug $pvp_user.pvp_death_logs %>

Mysql 数据库:pvp_users http://i.imgur.com/9LViwVG.png

Mysql 数据库:pvp_death_log http://i.imgur.com/A9WfPu4.png

【问题讨论】:

  • 我看到了一些可能导致问题的非风格指南/惯例。首先,模型是驼峰式的,没有下划线。秒,你有点误用flash。三、为什么要用全局变量$user而不是实例变量(@user)?
  • 谢谢,你说的我改了。我还必须在我的表名末尾添加一个“s”。现在可以了。
  • Rails 有很多“神奇发生在这里”的技巧,你只需要习惯。不过,一旦你学会了所有这些,它就很容易使用了。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

您的类的命名与 Ruby 标准不一致。类和模块名称应采用驼峰命名法:

class PvpUser
  # (...)
end

class PvpDeathLog
  # (...)
end

【讨论】:

  • 我现在已经改变了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 2018-10-19
  • 2012-04-17
  • 2015-05-09
相关资源
最近更新 更多