新手问题 添加字段后,启动,本地跑起来一点问题都没有,,放到线上发布后就报错

Allen_hui · 2019年03月06日 · 最后由 Allen_hui 回复于 2019年03月06日 · 1155 次阅读

比如说我生成添加一个 column_type 字段,model,controller,views 里该添加的地方都添加了,本地跑起来没有任何问题,,但发布到线上就报 ActionView::Template::Error..................undefined method `column_type' for #Class:0x000000059828a8 Did you mean? columns.............的错误?本人新手一个,求大神指导一下

贴上相关代码吧

adamshen 回复

controller.rb

def new
    @course = Course.new
  end
  private

  def course_params
    params[:course][:price] = 0 if params[:course][:price].blank?
    params.require(:course).permit(:topic, :cover, :introduction, :column_type)
  end

_form.html.erb

<div class="form-group row <%= f.object.errors.include?(:column_type) ? 'has-danger' : '' %>">
            <label class="col-md-2 form-control-label" for="select">
              所属栏目
            </label>
            <div class="col-md-10 has-danger">
              <%= f.select :column_type, options_for_select(Course.column_type.options, @course.column_type), {include_blank: '请选择'}, class: 'select2 form-control' %>
            </div>
          </div>

model.rb

class Course < ApplicationRecord
  acts_as_paranoid
  extend Enumerize
  enumerize :column_type, in: { hot_spot: 1, life: 2}

看代码好像没问题,应该是你在线上没有进行 rails db:migrate RAILS_ENV=production 导致线上数据库没有这个字段。

adamshen 回复

部署线上是用 cap 一键部署的,去线上的数据库里面查看了,但这个字段已经有了,还是报错。。这就是这个问题的奇怪之处,弄了好久都没弄明白咋回事

没环境调试不知道问题在哪。提醒一下 ActiveRecord 自带 enum https://api.rubyonrails.org/v5.2.2/classes/ActiveRecord/Enum.html

Allen_hui 回复

Sorry。没有仔细看出错信息。这里#Class:0x000000059828a8 没有显示出具体的 class,所以是 class_level 的 error。你对下出错信息里面的行号,是不是这一行。

<%= f.select :column_type, options_for_select(Course.column_type.options, @course.column_type), {include_blank: '请选择'}, class: 'select2 form-control' %>   <%= f.select :column_type, options_for_select(Course.column_type.options, @course.column_type), {include_blank: '请选择'}, class: 'select2 form-control' %>

那就是 Course.column_type 报的错,你可以具体排查一下为什么 enumerize :column_type 这个 dsl 没有给 Course define column_type 这个方法。

adamshen 回复

对的,就是 Course.column_type 这个地方出的问题,终于顺着这个地方去排查,找到原因了,感谢!!

Rei 回复

嗯嗯,感谢提醒

需要 登录 后方可回复, 如果你还没有账号请 注册新账号