【问题标题】:ActiveRecord model not fully instantiating after migration on Heroku在 Heroku 上迁移后 ActiveRecord 模型未完全实例化
【发布时间】:2016-11-15 03:48:30
【问题描述】:

在尝试为新实例化的 ActiveRecord 模型显示表单时出现以下错误。

ActionView::Template::Error (undefined method `title' for 
        #<Project id: nil, created_at: nil, updated_at: nil>):

控制器中的方法很简单

# GET /projects/new
def new
    @project = Project.new
end

看起来对象只有基本的 ActiveRecord 访问器,但应该有标题和其他一些。我pg:psql'ed 进入我的数据库并检查了结构并且它们在那里,所以迁移看起来已经完成。 问题是从表单访问title会抛出上述错误。

我的迁移:

class AddTitleBodyToProjects < ActiveRecord::Migration
    def change
        add_column :projects, :logo_url, :string
        add_column :projects, :title, :string
        add_column :projects, :body, :text
        add_column :projects, :web_url, :string
        add_column :projects, :github_url, :string
        add_column :projects, :appstore_url, :string
        add_column :projects, :playstore_url, :string
    end
end

我的 haml 模板(会引发错误):

.admin-form.work-form
= form_for @project, url: { action: @action, id: @project }, class: 'form-horizontal' do |f|
    .form-group.row
        = f.label :title, class: 'col-sm-2 control-label'
        .col-sm-9
            = f.text_field :title, class: 'form-control'
    .form-group.row
        = f.label :body, class: 'col-sm-2 control-label'
        .col-sm-9
            = f.text_area :body, class: 'form-control'
    .form-group.row
        = f.label :logo_url, class: 'col-sm-2 control-label'
        .col-sm-9
            = f.text_field :logo_url, class: 'form-control'
    .form-group.row
        = f.label :web_url, class: 'col-sm-2 control-label'
        .col-sm-9
            = f.text_field :web_url, class: 'form-control'
    .form-group.row
        = f.label :github_url, class: 'col-sm-2 control-label'
        .col-sm-9
            = f.text_field :github_url, class: 'form-control'
    .form-group.row
        = f.label :appstore_url, class: 'col-sm-2 control-label'
        .col-sm-9
            = f.text_field :appstore_url, class: 'form-control'
    .form-group.row
        = f.label :playstore_url, class: 'col-sm-2 control-label'
        .col-sm-9
            = f.text_field :playstore_url, class: 'form-control'
    .form-group.row
        .col-sm-offset-2.col-sm-9
            = f.submit 'Save', class: 'btn btn-default'

在本地一切正常,它只在抛出错误的 heroku 实例上。

【问题讨论】:

  • 您是否在 Heroku heroku run rake db:migrate 上运行了迁移?
  • @Leito 是的,运行了迁移,这就是为什么我提到我检查了 Postgres 表并且它具有我期望的字段
  • 你没有提到整个专栏(以“heroku”开头)所以我不确定。

标签: ruby-on-rails ruby activerecord heroku


【解决方案1】:

一旦打开heroku控制台并通过以下命令检查模型的列,

    heroku run rails c
    Project.first

如果在生成的对象中看不到标题列,请再次迁移并重新启动 heroku

    heroku run rake db:migrate
    heroku restart

【讨论】:

  • 正如我在问题中所说,我已经运行了迁移并且我已经检查了数据库表结构
【解决方案2】:

正如@diego.greyrobot 所说,这是一个缓存问题,运行heroku restart(然后等待几分钟)似乎已经解决了它。

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 2016-12-01
    • 2017-05-19
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    相关资源
    最近更新 更多