[英]Ruby on rails app works locally but does not work on heroku
I am doing michael hartl's tutorial for ruby on rails and everything works fine locally but not on heroku when the app deploys.. this problem dint arise before chapter 7 of this book: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#top 我正在做michael hartl关于ruby on rails的教程,一切都在本地工作正常,但在应用程序部署时没有在heroku上...这个问题出现在本书第7章之前: http : //ruby.railstutorial.org/ruby-on- Rails的教程书#顶部
are there any details you need to debug this problem? 你需要调试这个问题的细节吗? please let me know.....
请告诉我.....
here are the heroku logs: 这是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]:
So I was also doing the Hart Tutorial and was coming into the SAME issue. 所以我也在做Hart教程并且正在进入相同的问题。 I ran the heroku run db:migrate....i made sure it was up to date with git push heroku....I added a remember_key to my one user in the database.
我运行了heroku运行db:migrate ....我确保它是最新的git push heroku ....我在数据库中为我的一个用户添加了一个remember_key。 It was in the schema.rb file.
它位于schema.rb文件中。
I finally found the answer: 我终于找到了答案:
user.rb: user.rb:
add :remember_token to attr_accessible add:remember_token to attr_accessible
Ex: "attr_accessible :name, :email, :password, :password_confirmation, :remember_token" 例如:“attr_accessible:name,:email,:password,:password_confirmation,:remember_token”
For some reason this is left out of the tutorial, even though now that i know whats wrong it makes sense. 出于某种原因,这是在教程之外,即使现在我知道什么是错的,这是有道理的。 After adding this I re-pushed to heroku and Viola....
添加这个后我重新推到了heroku和Viola ....
Hope this helps :) 希望这可以帮助 :)
Dynamic finders only work if the attribute you're searching for exists. 动态查找程序仅在您搜索的属性存在时才有效。 Either you didn't run all migrations or you simply don't have that attribute (remember_token) or you misspelled it.
您没有运行所有迁移,或者您只是没有该属性(remember_token)或者您拼错了它。
I had the same problem and I've to say that IMHO it is not necessary to add :remember_token
to the attr_accessible
field. 我有同样的问题,我要说恕我直言,没有必要添加
:remember_token
到attr_accessible
字段。 Doing that you expose the :remember_token
, creating getters and setters, which is not necessary since :remember_token
has to be considered as an internal variable. 这样做会暴露
:remember_token
,创建getter和setter,这是必要的,因为:remember_token
必须被视为内部变量。 I solved the problem restarting the Heroku app, as confirmed here: 我解决了重新启动Heroku应用程序的问题,如下所示:
heroku-nomethod-error-remember-token Heroku的-nomethod错误记忆的令牌
After rebooting the app was working correctly, as it was doing locally. 重新启动后,应用程序正常工作,因为它在本地执行。
It fails because it does not find the definition of the following method find_by_remember_token
. 它失败,因为它找不到以下方法
find_by_remember_token
的定义。 I remember that remember_token
is defined in the session_helper
in the Hartl tutorial. 我记得
remember_token
是在Hartl教程的session_helper
中定义的。 It seems to arise when the signed_in?
这似乎出现在
signed_in?
method is called. 方法被调用。
Could you paste the session_helper.rb
and User
model code? 你能粘贴
session_helper.rb
和User
模型代码吗?
Try running heroku restart
. 尝试运行
heroku restart
。 It did the trick for me. 它为我做了伎俩。
It should be noted that adding :remembered_token
to your attr_accessible
declaration is not recommended, as per r4m's answer . 应该注意的是,根据r4m的答案 ,不建议在你的
attr_accessible
声明中添加:remembered_token
。 (There's a reason Hartl didn't do that in the tutorial.) (Hartl在教程中没有这样做是有原因的。)
The line: 这条线:
ActionView::Template::Error (undefined method `find_by_remember_token' for #<Class:0x00000004960740>):
is telling you that Rails doesn't know what 'find_by_remember_token' is, which means that it either doesn't exist (ie it's a typo), or that the database, which is generally used to determine class properties, isn't aware of this field. 告诉你Rails不知道'find_by_remember_token'是什么,这意味着它不存在(即它是一个错字),或者通常用于确定类属性的数据库不知道这个领域。
Assuming it's not a typo, then migrating your database would be an option: 假设它不是拼写错误,那么迁移数据库将是一个选项:
heroku run rake db:migrate
I was getting the internal server error when I tried to push to heroku and also to amazon. 当我尝试推送到heroku以及亚马逊时,我收到了内部服务器错误。 I had to precompile my assets before pushing the app to the server.
在将应用程序推送到服务器之前,我必须预先编译我的资产。 If this is your problem the easiest solution was to run
如果这是您的问题,最简单的解决方案就是运行
RAILS_ENV=production rake assets:precompile
or add load 'deploy/assets' to the capfile, if you are using Capistrano. 或者如果您使用Capistrano,则将负载'deploy / assets'添加到capfile。 I was told you can add the following to your deploy.rb file, but I was not able to get it working.
我被告知你可以将以下内容添加到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
What worked for me was defining the create_remember_token method in the User model exactly as described in the tutorial. 对我来说有用的是完全按照教程中的描述在User模型中定义create_remember_token方法。
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
And then calling it in the before_save method. 然后在before_save方法中调用它。
before_save {
self.email.downcase!
create_remember_token
}
I didn't have it as a method declaration before. 我之前没有将它作为方法声明。 And while it worked on my computer, it failed in Heroku.
虽然它在我的计算机上运行,但它在Heroku中失败了。
#this failed in Heroku
before_save {
self.email.downcase!
self.remember_token = SecureRandom.urlsafe_base64
}
Also, it would do no harm to run: 此外,运行不会有害:
heroku run rake db:migrate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.