无法在Heroku上获得在rails上工作的资源

时间:2018-01-10 22:07:49

标签: ruby-on-rails ruby heroku

我是一名铁路新手,在Heroku上部署时无法让resources在轨道上工作。它适用于我的开发环境(Cloud9),但不适用于Heroku。我的routes.rb如下:

Rails.application.routes.draw do
  root 'pages#home'
  get '/home', to: 'pages#home'

  resources :characters
end

当我在Cloud9上运行它时,它可以正常工作,rake routes返回:

        Prefix Verb   URI Pattern                    Controller#Action
          root GET    /                              pages#home
          home GET    /home(.:format)                pages#home
               GET    /home(.:format)                pages#home
    characters GET    /characters(.:format)          characters#index
               POST   /characters(.:format)          characters#create
 new_character GET    /characters/new(.:format)      characters#new
edit_character GET    /characters/:id/edit(.:format) characters#edit
     character GET    /characters/:id(.:format)      characters#show
               PATCH  /characters/:id(.:format)      characters#update
               PUT    /characters/:id(.:format)      characters#update
               DELETE /characters/:id(.:format)      characters#destroy

但是在Heroku上,我收到一条错误消息,说We're sorry, but something went wrong. If you are the application owner check the logs for more information.,日志显示给我:

2018-01-10T21:58:43.431137+00:00 app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 4.1ms)
2018-01-10T21:58:43.433165+00:00 app[web.1]:                 FROM pg_attribute a LEFT JOIN pg_attrdef d
2018-01-10T21:58:43.433157+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "characters" does not exist
2018-01-10T21:58:43.433168+00:00 app[web.1]:                WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:43.433154+00:00 app[web.1]: 
2018-01-10T21:58:43.433166+00:00 app[web.1]:                   ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2018-01-10T21:58:43.433159+00:00 app[web.1]:                                           ^
2018-01-10T21:58:43.433174+00:00 app[web.1]: 
2018-01-10T21:58:43.433169+00:00 app[web.1]:                ORDER BY a.attnum
2018-01-10T21:58:43.425295+00:00 app[web.1]: Processing by CharactersController#new as HTML
2018-01-10T21:58:43.433172+00:00 app[web.1]:   app/controllers/characters_controller.rb:7:in `new'
2018-01-10T21:58:43.433158+00:00 app[web.1]: LINE 5:                WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:43.433170+00:00 app[web.1]: ):
2018-01-10T21:58:43.422364+00:00 app[web.1]: Started GET "/characters/new" for 24.155.28.170 at 2018-01-10 21:58:43 +0000
2018-01-10T21:58:43.433168+00:00 app[web.1]:                  AND a.attnum > 0 AND NOT a.attisdropped
2018-01-10T21:58:43.433161+00:00 app[web.1]: :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2018-01-10T21:58:43.433164+00:00 app[web.1]:                      pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
2018-01-10T21:58:43.433173+00:00 app[web.1]: 
2018-01-10T21:58:58.637394+00:00 app[web.1]:                      pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
2018-01-10T21:58:58.637395+00:00 app[web.1]:                 FROM pg_attribute a LEFT JOIN pg_attrdef d
2018-01-10T21:58:58.626386+00:00 app[web.1]: Started GET "/characters/new" for 24.155.28.170 at 2018-01-10 21:58:58 +0000
2018-01-10T21:58:58.637397+00:00 app[web.1]:                WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:58.637402+00:00 app[web.1]: 
2018-01-10T21:58:58.637392+00:00 app[web.1]: :               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2018-01-10T21:58:58.637389+00:00 app[web.1]: LINE 5:                WHERE a.attrelid = '"characters"'::regclass
2018-01-10T21:58:58.637400+00:00 app[web.1]: ):
2018-01-10T21:58:58.637401+00:00 app[web.1]:   app/controllers/characters_controller.rb:7:in `new'
2018-01-10T21:58:58.635689+00:00 app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 4.0ms)
2018-01-10T21:58:58.637399+00:00 app[web.1]:                ORDER BY a.attnum
2018-01-10T21:58:58.637385+00:00 app[web.1]: 
2018-01-10T21:58:58.637391+00:00 app[web.1]:                                           ^
2018-01-10T21:58:58.637396+00:00 app[web.1]:                   ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2018-01-10T21:58:58.637398+00:00 app[web.1]:                  AND a.attnum > 0 AND NOT a.attisdropped
2018-01-10T21:58:58.629873+00:00 app[web.1]: Processing by CharactersController#new as HTML
2018-01-10T21:58:58.637388+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "characters" does not exist
2018-01-10T21:58:58.637403+00:00 app[web.1]: 

出了什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

假设你没有做任何关于放置你的mdoels的地方,ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "characters" does not exist表示没有将这个表放在PostgreSQL中。

heroku run rake db:migrate应该可以解决问题。