我遇到了从Heroku本地开发到生产的问题。除了我的“留言簿”控制器中的一个“新”动作之外,一切都很好。当我尝试导航到https://allbugsaside.herokuapp.com/guestbooks/new时,我收到一个错误,告诉我查看日志。当我运行Heroku日志时,除了发起请求之外,它并没有告诉我多少。
我有一种感觉,我可能会遇到这个问题,因为在我的“新”行动中,我打电话给。来自留言簿目前没有任何数据?只是一个理论。
任何人都可以协助解决这个问题吗?感谢。
class GuestbooksController < ApplicationController
def index
@guestbooks = Guestbook.all
end
def new
@guestbook = Guestbook.new
@guestbooks = Guestbook.all
@guestbooks = Kaminari.paginate_array(@guestbooks).page(params[:page]).per(5)
end
def create
@guestbook = Guestbook.new(guestbook_params)
# @guestbooks = Guestbook.all.limit(1).page(params[:page])
if @guestbook.save
flash.now[:notice] = "Thanks for taking the time to write us! We greatly appreciate it!"
render :new
else
flash.now[:notice] = "Your message failed to post, please try again"
render :new
end
end
private
def guestbook_params
params.require(:guestbook).permit(:name, :email, :message)
end
end
这是我的new.html.erb
<h1 class="guestbook_head">Guestbook</h1><br/>
<div class="guestbook_text">
<p>
It is our goal to provide our customers with the best possible Pest Control Services and Solutions in the industry.
Your comments and feedback are important to us. They are a tool we use in order to keep on improving our experience, with you ….the customer. <br/><br/>
</p>
</div>
<div class="paginate">
<%= paginate @guestbooks %>
</div>
<div class="span1">
<% @guestbooks.each do |g| %>
<br/>
<h4><%= g.name %>, <%= g.created_at %><br/></h4>
<%= g.message %><br/>
<p>-----------------------------------------------------------------------------------------------------------------------</p>
<% end %>
</div>
<%= simple_form_for @guestbook, url: guestbooks_path, method: :post do |f| %>
<div class="span2"><%= f.input :name %></div>
<div class="span2"><%= f.input :email %></div>
<div class="span3"><%= f.input :message %></div>
<div class="span4"><%= f.button :submit %></div><br/><br/><br/>
<% end %>
这些是我的路线;
Rails.application.routes.draw do
resources :pages, only: [:index]
get "/pages/home", to: "pages#home"
get "/pages/about", to: "pages#about"
get "/pages/pest_info", to: "pages#pest_info"
get "/pages/annual_contracts", to: "pages#annual_contracts"
get "/pages/monthly_info", to: "pages#monthly_info"
get "/pages/snowplow", to: "pages#snowplow"
get "/pages/gallery", to: "pages#gallery"
resources :contact_form, only: [:new, :create]
resources :guestbooks, only: [:new, :create]
get "/guestbooks/index", to: "guestbooks#index"
root "pages#home"
end
当我尝试访问该页面时,我收到500内部服务器错误。
2015-12-06T18:42:33.507953+00:00 heroku[router]: at=info method=GET path="/pages/about" host=allbugsaside.herokuapp.com request_id=e07095da-734c-4fe3-aaa8-6f68d86a8e20 fwd="96.248.110.224" dyno=web.1 connect=0ms service=7ms status=200 bytes=5022
2015-12-06T18:42:40.331424+00:00 heroku[router]: at=info method=GET path="/guestbooks/new" host=allbugsaside.herokuapp.com request_id=134c9aab-f559-4e95-bad5-c1037dd38291 fwd="96.248.110.224" dyno=web.1 connect=0ms service=8ms status=500 bytes=1754
2015-12-06T18:42:40.390097+00:00 heroku[router]: at=info method=GET path="/guestbooks/new" host=allbugsaside.herokuapp.com request_id=d552b372-80fa-4e76-83a3-371aa4f994ab fwd="96.248.110.224" dyno=web.1 connect=0ms service=7ms status=500 bytes=1754
答案 0 :(得分:0)
您是否已完成heroku run rake db:migrate
以更新数据库?当然,只有在本地环境中进行了新的迁移。如果这不能解决您的问题,请发布heroku的日志。
请记住,Rails约定使用index
(在控制器中查看和def)来显示某些模型所需的所有对象。
在这里,您希望在new
- 对象视图中显示它们。这是可能的,请小心。您的观点是new.html.erb
吗?
另请注意,def new
不是一个操作,它只是您的new.html.erb
的控制器,它为视图提供数据,变量,重定向和其他内容。
答案 1 :(得分:0)
我注意到您的页面/留言簿没有响应索引操作。你可以发布routes.rb
Ruby技术上是动态的:
<div class="container">
<div class="header">
<h1>Summary</h1></div>
<table class="table">
<caption>Use inline rows.</caption>
<tbody>
<tr>
<th scope="row">Account Name: </th>
<td>my_acc </td>
<td style="font-weight: bold">Last Payment: </td>
<td>100USD</td>
</tr>
<tr>
<th scope="row">Account Balance: </th>
<td>$10</td>
<td style="font-weight: bold">Last Payment Date: </td>
<td>11/25/2434</td>
</tr>
<tr>
<th scope="row">Last Invoice: </th>
<td>Larry</td>
</tr>
</tbody>
</table>
</div>
但我不会这样做。重新定义变量并将其用作声明的引用与该句子一样难以阅读。
@guestbooks = Guestbook.all
@guestbooks = Kaminari.paginate_array(@guestbooks).page(params[:page]).per(5)
如果问题存在于生产中而不是dev中,则发布的代码不存在问题。这不是数据库中缺少数据的问题,但它可能是架构的问题。