Ruby on Rails应用程序可以在本地运行,但不能在heroku上运行

mnowg1ta  于 2022-11-13  发布在  Ruby
关注(0)|答案(8)|浏览(178)

我正在做michael hartl的ruby on rails教程,当应用程序部署时,本地一切都很好,但在heroku上就不行了......这个问题在本书第7章之前就出现了:http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#top
是否有任何细节你需要调试这个问题?请让我知道.....
下面是heroku日志:

$ 2012-07-18T06:10:26+00:00 app[web.1]:     6:         <ul class="nav pull-right">

2012-07-18T06:10:26+00:00 app[web.1]: ActionView::Template::Error (undefined met
hod `find_by_remember_token' for #<Class:0x00000004960740>):
2012-07-18T06:10:26+00:00 app[web.1]:     7:           <li><%= link_to "Home", r
oot_path %></li>
2012-07-18T06:10:26+00:00 app[web.1]:     8:           <li><%= link_to "Help", h
elp_path %></li>
2012-07-18T06:10:26+00:00 app[web.1]:     9:           <% if signed_in? %>
2012-07-18T06:10:26+00:00 app[web.1]:     10:             <li><%= link_to "Users
", users_path %></li>
2012-07-18T06:10:26+00:00 app[web.1]:     11:             <li id="fat-menu" clas
s="dropdown">
2012-07-18T06:10:26+00:00 app[web.1]:     12:               <a href="#" class="d
ropdown-toggle" data-toggle="dropdown">
2012-07-18T06:10:26+00:00 app[web.1]:   app/helpers/sessions_helper.rb:10:in `cu
rrent_user'
2012-07-18T06:10:26+00:00 app[web.1]:   app/helpers/sessions_helper.rb:23:in `si
gned_in?'
2012-07-18T06:10:26+00:00 app[web.1]:   app/views/layouts/application.html.erb:1
1:in `_app_views_layouts_application_html_erb___2533055855927805914_43132200'
2012-07-18T06:10:26+00:00 app[web.1]:   app/views/layouts/_header.html.erb:9:in
`_app_views_layouts__header_html_erb__2547305690965663969_41244280'
2012-07-18T06:10:26+00:00 app[web.1]:
2012-07-18T06:10:26+00:00 app[web.1]:
2012-07-18T06:10:26+00:00 heroku[router]: GET whizcollab.herokuapp.com/ dyno=web
.1 queue=0 wait=0ms service=345ms status=500 bytes=643
2012-07-18T06:10:34+00:00 app[web.1]:
2012-07-18T06:10:34+00:00 app[web.1]: Started GET "/" for 115.87.214.246 at 2012
-07-18 06:10:34 +0000
2012-07-18T06:10:34+00:00 app[web.1]:
2012-07-18T06:10:34+00:00 app[web.1]: Processing by StaticPagesController#home a
s HTML
2012-07-18T06:10:34+00:00 app[web.1]:   Rendered static_pages/home.html.erb with
in layouts/application (0.6ms)
2012-07-18T06:10:34+00:00 app[web.1]:   Rendered layouts/_shim.html.erb (0.0ms)
2012-07-18T06:10:34+00:00 app[web.1]:   Rendered layouts/_header.html.erb (1.2ms
)
2012-07-18T06:10:34+00:00 app[web.1]: Completed 500 Internal Server Error in 4ms

2012-07-18T06:10:34+00:00 app[web.1]: ActionView::Template::Error (undefined met
hod `find_by_remember_token' for #<Class:0x00000004960740>):
2012-07-18T06:10:34+00:00 app[web.1]:
2012-07-18T06:10:34+00:00 app[web.1]:     8:           <li><%= link_to "Help", h
elp_path %></li>
2012-07-18T06:10:34+00:00 app[web.1]:     9:           <% if signed_in? %>
2012-07-18T06:10:34+00:00 app[web.1]:     6:         <ul class="nav pull-right">

2012-07-18T06:10:34+00:00 app[web.1]:     10:             <li><%= link_to "Users
", users_path %></li>
2012-07-18T06:10:34+00:00 app[web.1]:     7:           <li><%= link_to "Home", r
oot_path %></li>
2012-07-18T06:10:34+00:00 app[web.1]:     11:             <li id="fat-menu" clas
s="dropdown">
2012-07-18T06:10:34+00:00 app[web.1]:     12:               <a href="#" class="d
ropdown-toggle" data-toggle="dropdown">
2012-07-18T06:10:34+00:00 app[web.1]:   app/helpers/sessions_helper.rb:10:in `cu
rrent_user'
2012-07-18T06:10:34+00:00 app[web.1]:   app/views/layouts/_header.html.erb:9:in
`_app_views_layouts__header_html_erb__2547305690965663969_41244280'
2012-07-18T06:10:34+00:00 app[web.1]:   app/helpers/sessions_helper.rb:23:in `si
gned_in?'
2012-07-18T06:10:34+00:00 app[web.1]:   app/views/layouts/application.html.erb:1
1:in `_app_views_layouts_application_html_erb___2533055855927805914_43132200'
2012-07-18T06:10:34+00:00 app[web.1]:
2012-07-18T06:10:34+00:00 app[web.1]:
2012-07-18T06:10:34+00:00 heroku[router]: GET whizcollab.herokuapp.com/ dyno=web
.1 queue=0 wait=0ms service=22ms status=500 bytes=643
2012-07-18T06:12:03+00:00 app[web.1]:
2012-07-18T06:12:03+00:00 app[web.1]:
2012-07-18T06:12:03+00:00 app[web.1]: Started GET "/" for 115.87.214.246 at 2012
-07-18 06:12:03 +0000
2012-07-18T06:12:03+00:00 app[web.1]: Processing by StaticPagesController#home a
s HTML
2012-07-18T06:12:03+00:00 app[web.1]:   Rendered static_pages/home.html.erb with
in layouts/application (0.7ms)
2012-07-18T06:12:03+00:00 app[web.1]:   Rendered layouts/_shim.html.erb (0.0ms)
2012-07-18T06:12:03+00:00 app[web.1]: Completed 500 Internal Server Error in 6ms

2012-07-18T06:12:03+00:00 app[web.1]:   Rendered layouts/_header.html.erb (2.3ms
)
2012-07-18T06:12:03+00:00 app[web.1]: ActionView::Template::Error (undefined met
hod `find_by_remember_token' for #<Class:0x00000004960740>):
2012-07-18T06:12:03+00:00 app[web.1]:
2012-07-18T06:12:03+00:00 app[web.1]:     6:         <ul class="nav pull-right">

2012-07-18T06:12:03+00:00 app[web.1]:     7:           <li><%= link_to "Home", r
oot_path %></li>
2012-07-18T06:12:03+00:00 app[web.1]:     8:           <li><%= link_to "Help", h
elp_path %></li>
2012-07-18T06:12:03+00:00 app[web.1]:     9:           <% if signed_in? %>
2012-07-18T06:12:03+00:00 app[web.1]:     11:             <li id="fat-menu" clas
s="dropdown">
2012-07-18T06:12:03+00:00 app[web.1]:     10:             <li><%= link_to "Users
", users_path %></li>
2012-07-18T06:12:03+00:00 app[web.1]:     12:               <a href="#" class="d
ropdown-toggle" data-toggle="dropdown">
2012-07-18T06:12:03+00:00 app[web.1]:   app/helpers/sessions_helper.rb:10:in `cu
rrent_user'
2012-07-18T06:12:03+00:00 app[web.1]:   app/helpers/sessions_helper.rb:23:in `si
gned_in?'
2012-07-18T06:12:03+00:00 app[web.1]:   app/views/layouts/_header.html.erb:9:in
`_app_views_layouts__header_html_erb__2547305690965663969_41244280'
2012-07-18T06:12:03+00:00 app[web.1]:   app/views/layouts/application.html.erb:1
1:in `_app_views_layouts_application_html_erb___2533055855927805914_43132200'
2012-07-18T06:12:03+00:00 app[web.1]:
2012-07-18T06:12:03+00:00 app[web.1]:
2012-07-18T06:12:03+00:00 heroku[router]: GET whizcollab.herokuapp.com/ dyno=web
.1 queue=0 wait=0ms service=17ms status=500 bytes=643
2012-07-18T06:13:31+00:00 app[web.1]:
2012-07-18T06:13:31+00:00 app[web.1]:
2012-07-18T06:13:31+00:00 app[web.1]: Started GET "/" for 115.87.214.246 at 2012
-07-18 06:13:31 +0000
2012-07-18T06:13:31+00:00 app[web.1]: Processing by StaticPagesController#home a
s HTML
2012-07-18T06:13:31+00:00 app[web.1]:   Rendered static_pages/home.html.erb with
in layouts/application (0.4ms)
2012-07-18T06:13:31+00:00 app[web.1]:   Rendered layouts/_shim.html.erb (0.0ms)
2012-07-18T06:13:31+00:00 app[web.1]:   Rendered layouts/_header.html.erb (1.9ms
)
2012-07-18T06:13:31+00:00 app[web.1]: Completed 500 Internal Server Error in 4ms

2012-07-18T06:13:31+00:00 app[web.1]:
2012-07-18T06:13:31+00:00 app[web.1]: ActionView::Template::Error (undefined met
hod `find_by_remember_token' for #<Class:0x00000004960740>):
2012-07-18T06:13:31+00:00 app[web.1]:     6:         <ul class="nav pull-right">

2012-07-18T06:13:31+00:00 app[web.1]:     7:           <li><%= link_to "Home", r
oot_path %></li>
2012-07-18T06:13:31+00:00 app[web.1]:     9:           <% if signed_in? %>
2012-07-18T06:13:31+00:00 app[web.1]:     11:             <li id="fat-menu" clas
s="dropdown">
2012-07-18T06:13:31+00:00 app[web.1]:     10:             <li><%= link_to "Users
", users_path %></li>
2012-07-18T06:13:31+00:00 app[web.1]:     8:           <li><%= link_to "Help", h
elp_path %></li>
2012-07-18T06:13:31+00:00 app[web.1]:     12:               <a href="#" class="d
ropdown-toggle" data-toggle="dropdown">
2012-07-18T06:13:31+00:00 app[web.1]:   app/helpers/sessions_helper.rb:10:in `cu
rrent_user'
2012-07-18T06:13:31+00:00 app[web.1]:   app/helpers/sessions_helper.rb:23:in `si
gned_in?'
2012-07-18T06:13:31+00:00 app[web.1]:   app/views/layouts/application.html.erb:1
1:in `_app_views_layouts_application_html_erb___2533055855927805914_43132200'
2012-07-18T06:13:31+00:00 app[web.1]:
2012-07-18T06:13:31+00:00 app[web.1]:   app/views/layouts/_header.html.erb:9:in
`_app_views_layouts__header_html_erb__2547305690965663969_41244280'
2012-07-18T06:13:31+00:00 app[web.1]:
2012-07-18T06:13:31+00:00 heroku[router]: GET whizcollab.herokuapp.com/ dyno=web
.1 queue=0 wait=0ms service=22ms status=500 bytes=643
2012-07-18T06:13:37+00:00 app[web.1]:
2012-07-18T06:13:37+00:00 app[web.1]:
2012-07-18T06:13:37+00:00 app[web.1]: Started GET "/users" for 115.87.214.246 at
 2012-07-18 06:13:37 +0000
2012-07-18T06:13:37+00:00 heroku[router]: GET whizcollab.herokuapp.com/users dyn
o=web.1 queue=0 wait=0ms service=47ms status=500 bytes=643
2012-07-18T06:13:37+00:00 app[web.1]: Processing by UsersController#index as HTM
L
2012-07-18T06:13:37+00:00 app[web.1]: Completed 500 Internal Server Error in 1ms

2012-07-18T06:13:37+00:00 app[web.1]:
2012-07-18T06:13:37+00:00 app[web.1]:   app/helpers/sessions_helper.rb:10:in `cu
rrent_user'
2012-07-18T06:13:37+00:00 app[web.1]: NoMethodError (undefined method `find_by_r
emember_token' for #<Class:0x00000004960740>):
2012-07-18T06:13:37+00:00 app[web.1]:   app/helpers/sessions_helper.rb:23:in `si
gned_in?'
2012-07-18T06:13:37+00:00 app[web.1]:   app/controllers/users_controller.rb:48:i
n `signed_in_user'
2012-07-18T06:13:37+00:00 app[web.1]:
2012-07-18T06:13:37+00:00 app[web.1]:
hof1towb

hof1towb1#

我在做哈特教程的时候也遇到了同样的问题。我运行了heroku run db:migrate....我确保它是最新的git push heroku....我在数据库中为我的一个用户添加了remember_key。它在schema.rb文件中。
我终于找到了答案:
user.rb:
添加:记住令牌到attr_accessible
例如:“attr_accessible:名称,:电子邮件,:密码,:密码确认,:记住令牌”
由于某些原因,这是遗漏的教程,即使现在我知道什么是错的,这是有意义的。
希望这对你有帮助:)

l7mqbcuq

l7mqbcuq2#

动态查找器仅在您要搜索的属性存在时才起作用。可能是您没有运行所有迁移,也可能是您根本没有该属性(remember_token)或拼错了该属性。

mdfafbf1

mdfafbf13#

我遇到了同样的问题,我不得不说,恕我直言,没有必要将:remember_token添加到attr_accessible字段。这样做会暴露:remember_token,创建getter和setter,这是不必要的,因为:remember_token必须被视为内部变量。我解决了重新启动Heroku应用程序的问题,如下所示:
heroku-nomethod-error-remember-token
重新启动后,应用程序正常工作,就像它在本地做的那样。

wj8zmpe1

wj8zmpe14#

它失败了,因为它没有找到下面的find_by_remember_token方法的定义。我记得remember_token是在Hartl教程的session_helper中定义的。它似乎是在调用signed_in?方法时出现的。
能否粘贴session_helper.rbUser型号代码?

nxagd54h

nxagd54h5#

试着运行heroku restart。它对我很有效。
需要注意的是,根据r4m's answer,不建议将:remembered_token添加到attr_accessible声明中(Hartl在本教程中没有这样做是有原因的)。

fwzugrvs

fwzugrvs6#

行:

ActionView::Template::Error (undefined method `find_by_remember_token' for #<Class:0x00000004960740>):

Rails不知道'find_by_remember_token'是什么,这意味着它要么不存在(也就是说,它是一个拼写错误),要么通常用于确定类属性的数据库不知道这个字段。
假设这不是打字错误,那么迁移数据库将是一个选项:

heroku run rake db:migrate
c7rzv4ha

c7rzv4ha7#

当我尝试推送到Heroku和Amazon时,我遇到了内部服务器错误。在将应用程序推送到服务器之前,我必须预编译我的资产。如果这是您的问题,最简单的解决方案是运行

RAILS_ENV=production rake assets:precompile

或者添加load“deploy/assets”到capfile,如果您使用Capistrano的话。我被告知您可以将以下内容添加到您的deploy.rb文件中,但我无法让它工作。

after "deploy:restart", "deploy:precompile"

  namespace :deploy do

    desc "Compile assets"
    task :precompile, :roles => :app do
    run "cd #{release_path} && rake RAILS_ENV=#{rails_env} assets:precompile"
  end
end
7fhtutme

7fhtutme8#

对我有效的方法是在User模型中定义create_remember_token方法,与教程中描述的完全相同。

private
def create_remember_token
   self.remember_token = SecureRandom.urlsafe_base64
end

然后在before_保存方法中调用它。

before_save { 
    self.email.downcase!
    create_remember_token
}

我以前没有把它作为方法声明。当它在我的计算机上工作时,它在Heroku中失败了。

#this failed in Heroku    
before_save { 
    self.email.downcase!
    self.remember_token = SecureRandom.urlsafe_base64
}

而且,跑也无妨:

heroku run rake db:migrate

相关问题