2016-07-12 57 views
0

嘗試爲新實例化ActiveRecord模型顯示錶單時出現以下錯誤。ActiveRecord模型在Heroku上遷移後沒有完全實例化

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的實例。

+3

您是否在Heroku上運行遷移'heroku run rake db:migrate'? – Leito

+2

,然後等待幾秒鐘和/或運行'heroku restart',因爲有時需要刷新postgres緩存,以使新模式更改對您的運行dyno可見 – DiegoSalazar

+0

@Leito是一個運行遷移,這就是爲什麼我提到我檢查了Postgres表並且它有我期望的字段 – 92tonywills

回答

0

由於@ diego.greyrobot提到這是一個緩存問題,並運行heroku restart(然後等待幾分鐘)似乎已修復它。

1

一旦開放Heroku的控制檯,並通過以下命令來檢查模型中的列,

heroku run rails c 
    Project.first 

如果您看不到對象標題列生成然後再遷移和重新啓動的Heroku

heroku run rake db:migrate 
    heroku restart 
+0

正如我在問題中說我已經運行遷移,我已經檢查了數據庫表結構 – 92tonywills

相關問題