【发布时间】:2018-06-30 16:30:35
【问题描述】:
我正在尝试使用 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 或设置了不正确的东西,并且希望有一个有用的示例来从我的表中选择数据。
【问题讨论】:
-
只是一个愚蠢的问题:如果你变成字符串
'Post.all',它会打印到浏览器吗? -
Post.all似乎不起作用,但我设法找到了一种使用Post.to_yaml打印出条目的方法
标签: ruby nginx sinatra mariadb unicorn