我正在尝试使用ActiveRecords查询与Sinatra一起设置的MariaDB。
我的表架构如下
MariaDB [orbital]> select * from posts
-> ;
+----+------+------+----------+
| id | user | post | location |
+----+------+------+----------+
| 1 | 100 | 100 | 100 |
+----+------+------+----------+
1 row in set (0.00 sec)
Sinatra与Unicorn和Nginx一起设置为代理请求。
MyApp.rb
require 'rubygems'
require 'sinatra'
require 'active_record'
require 'table_print'
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "root",
:database => "orbital"
)
class Post < ActiveRecord::Base
end
class MyApp < Sinatra::Application
get '/' do
p Post.all
end
end
我使用Kurly来`kurly -X GET -k localhost
我收到以下错误:
HTTP/1.1 500 Internal Server Error
我尝试修改ruby文件,但是得到了不同的结果。使用table_print
将允许页面以0字节加载。我确定我使用了ActiveRecord错误或设置有误,并希望使用一个示例从表中选择数据。
答案 0 :(得分:1)
我建议您将代码捕获到异常中,并检查异常消息以查看是否通过简单的puts
引发了重要的事情。如果那不起作用,则可以使用调试器检查确切的错误和错误原因。
对establish_connection
方法的某些错误配置。凭证可能不正确或什么。
我使用的一个好的调试器是pry
,您可以将其添加到Gemfile中,将gem导入到控制器文件中,然后使用binding.pry
进入调试器模式。