Rails生产模式下的问题,mysql数据库
应用程序在开发模式下运行顺利,但将其部署到托管服务器上会显示 public/500.html 页面(“我们很抱歉,但出了点问题”),似乎当内容查询mysql数据库。放置静态内容是有效的,令人费解的是,动态内容只能在开发人员本地工作。
rake db:migrate 运行正常,我已手动将测试条目插入到 mysql 数据库中。
然而,请求视图结果为 500,日志显示
ActionView::Template::Error (NULL pointer given):
10: ...
11: <% @projects.each do |project| %>
12: ...
服务器运行 MySQL 5.1.54,Rails 安装了 mysql gem。 任何提示表示赞赏!
编辑:
所以我刚刚
启动rails c Production
在远程服务器上
并在我的“客户端”模型中创建了一个条目: irb(main):003:0> c = 客户端.new => #<客户端ID:nil,名称:nil,永久链接:nil,created_at:nil,updated_at:nil> irb(主):004:0> c.name = "真实客户端" => “真实客户” irb(主):005:0> c => #<客户端 ID:nil,名称:“realclient”,永久链接:nil,created_at:nil,updated_at:nil> irb(主):006:0> c.保存 =>真的
虽然除了 name 之外的所有字段在 irb 中都显示为空,但服务器上的实际数据库显示了所有相应的字段:
2 realclient realclient 2011-04-22 13:59: 12 2011-04-22 13:59:12
(id、名称、永久链接、创建、更新)
所以底线是:Active Record 无法从数据库中正确接收实际存在的值?
App is running smoothly in development mode, but deploying it onto a managed server brings up the public/500.html page ("We're sorry, but something went wrong"), seemingly when content queries the mysql database. Putting up static content works, puzzlingly dynamic content only works locally in dev.
rake db:migrate went properly, I've manually inserted a test entry into the mysql database.
Yet requesting the view results in 500 and the log gives
ActionView::Template::Error (NULL pointer given):
10: ...
11: <% @projects.each do |project| %>
12: ...
Server runs MySQL 5.1.54, Rails has mysql gem installed.
Any hints appreciated!
edit:
So I just started
rails c production
on the remote server and created an entry in my "Client" model:
irb(main):003:0> c = Client.new
=> #<Client id: nil, name: nil, permalink: nil, created_at: nil, updated_at: nil>
irb(main):004:0> c.name = "realclient"
=> "realclient"
irb(main):005:0> c
=> #<Client id: nil, name: "realclient", permalink: nil, created_at: nil, updated_at: nil>
irb(main):006:0> c.save
=> true
While all fields except for name are displayed empty in irb, the actual DB on the server show all repective fields okay:
2 realclient realclient 2011-04-22 13:59:12 2011-04-22 13:59:12
(id, name, permalink, created, updated)
So bottom line: Active Record cannot receive values correctly from the DB that are actually there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您运行
rake db:migrate
时,您可能没有设置RAILS_ENV
,因此仍在开发环境中运行。尝试运行:看看是否出现错误,如果是,您可以使用
--trace
选项重新运行它以获取更多详细信息。您可能需要在
config/database.yml
上调试此问题 - 生产 部分中可能会缺少某些内容或配置错误(或者您可能还没有创建DB之类的,我不熟悉那个特定的错误消息)。When you ran
rake db:migrate
you probably did not have aRAILS_ENV
set and so were still running in development environment. Try running:See if you get an error then, and if so you can re-run it with the
--trace
option for more detail.You probably need to debug this issue on your
config/database.yml
- there'll be something missing or misconfigured in the production section (or maybe you haven't created the DB or something, I'm not familiar with that specific error message).** 已解决 **
我的托管提供商只允许使用他们预装的 mysql 2.7 gem。在 Gemfile 中手动加载 mysql gem 会导致 Active Record 错误——甚至在顶部加载自己的 2.7 版本。坚持使用托管服务器上的默认 gem 是可行的。
在服务器上单独设置一个新数据库可能可以启用自定义数据库 gem。因此,虽然这是一个非常具体的问题,但也许答案可以帮助其他人在托管服务器上使用 Rails。
** Solved **
My hosting provider only allows use of their pre-installed mysql 2.7 gem. Any manual loading of a mysql gem in Gemfile results in Active Record errors – even loading an own 2.7 version on top. Sticking to the default gem on the managed server works.
Setting up a new database individually on the server could probably enable use of custom database gems. So while this was a very specific problem, maybe the answer helps somebody else working with rails on a managed server.