在Heroku

时间:2016-07-12 18:48:32

标签: ruby-on-rails ruby activerecord heroku

尝试显示新实例化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进入我的数据库并检查结构,他们在那里,所以迁移看起来已经完成。 问题是从表单中访问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实例上引发错误。

2 个答案:

答案 0 :(得分:1)

打开heroku控制台并按照以下命令检查模型的列

    heroku run rails c
    Project.first

如果在生成的对象中没有看到title列,则再次迁移并重新启动heroku

    heroku run rake db:migrate
    heroku restart

答案 1 :(得分:0)

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

相关问题