在我自己的迷你项目工作期间,我正在通过railstutorial。虽然有人建议您使用的开发数据库应与生产中的开发数据库相同,但本指南使用sqlite进行开发 我一直在做同样的事情。
我最近部署了一些在本地工作的更改到我在Heroku上托管的应用程序但是,当我尝试访问特定页面时,收到以下错误:
If you are the application owner check the logs for more information.
我在下面附上了我的代码。
accounts_controller.rb:
# GET /accounts/1/history
def history
@account = Account.find(params[:id])
@accountHistory = Account.select("accounts.*, balances.*").joins("JOIN balances").where('balances.account_id = :accountId', {accountId: @account.id}).where('accounts.id = :accountId', {accountId: @account.id}).order('date desc')
end
history.html.erb
<tbody>
<% @accountHistory.each do |a| %>
<tr>
<td><%= date_format(a.date) %></td>
<td class='account-balance'><%= number_to_currency(a.balance, unit: "£") %></td>
</tr>
<% end %>
</tbody>
更新
查看Heroku日志,看起来Postgres的语法错误。谁能建议如何改进呢?
heroku日志:
2015-12-22T16:54:39.361012+00:00 app[web.1]: Started GET "/accounts/3/history" for 2.220.200.172 at 2015-12-22 16:54:39 +0000
2015-12-22T16:54:39.370975+00:00 app[web.1]: Account Load (0.9ms) SELECT accounts.*, balances.* FROM "accounts" JOIN balances WHERE (balances.account_id = 3) AND (accounts.id = 3) ORDER BY date desc
2015-12-22T16:54:39.371012+00:00 app[web.1]: LINE 1: ...ounts.*, balances.* FROM "accounts" JOIN balances WHERE (bal...
2015-12-22T16:54:39.371010+00:00 app[web.1]: PG::SyntaxError: ERROR: syntax error at or near "WHERE"
2015-12-22T16:54:39.371013+00:00 app[web.1]: ^
2015-12-22T16:54:39.371015+00:00 app[web.1]: : SELECT accounts.*, balances.* FROM "accounts" JOIN balances WHERE (balances.account_id = 3) AND (accounts.id = 3) ORDER BY date desc
2015-12-22T16:54:39.371797+00:00 app[web.1]: Rendered accounts/history.html.erb within layouts/application (2.3ms)
2015-12-22T16:54:39.371929+00:00 app[web.1]: Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.6ms)
2015-12-22T16:54:39.374155+00:00 app[web.1]:
2015-12-22T16:54:39.374158+00:00 app[web.1]: ActionView::Template::Error (PG::SyntaxError: ERROR: syntax error at or near "WHERE"
答案 0 :(得分:1)
部署到heroku后,您是否运行了heroku run rake db:migrate -a <YOUR HEROKU APP NAME>
?
如果您这样做,您可以查看他们所指的日志:
heroku logs --tail -a <YOUR HEROKU APP NAME>
将<YOUR HEROKU APP NAME>
替换为heroku配置上的名称。如果您不知道如何找到它,请转到heroku.com仪表板并在设置下找到它。
答案 1 :(得分:1)
看起来Heroku错误与联接语句有关。它需要on
,你可以做几个方面。要在活动关系中执行此操作,只需从
.joins("JOIN balances")
要
.joins(:balances)
注意 - 可能不再需要此部分
.where('balances.account_id = :accountId', {accountId: @account.id})
进一步查看你的代码,可能还有其他你真正想要的东西。如果accountHistory
是balance
记录的列表,这对您有用....
@account = Account.find(params[:id])
@accountHistory = @account.balances
或者,可能在一个查询中执行...
@account = Account.includes(:balances).find(params[:id])
余额集合在帐户中被引用,我怀疑
class Account < ActiveRecord::Base
has_many :balances
end
并且,使用Balance上的外键
class Balance < ActiveRecord::Base
belongs_to :account
# Add a scope if you like to reuse code
scope :order_by_date, -> { order(:date => :desc) }
end
然后,您可以轻松参考余额。这就是Rails帮助你做很多事情以使事情变得可读的地方。
@account.balances.order_by_date
如果需要,您可以将其指定给@accountHistory
,或者只是在视图中的集合关闭帐户中引用balances
,如下所示:
<tbody>
<% @account.balances.order(:date => :desc).each do |b| %>
<tr>
<td><%= date_format(b.date) %></td>
<td class='account-balance'><%= number_to_currency(b.balance, unit: "£") %></td>
</tr>
<% end %>
</tbody>