【发布时间】:2015-12-06 17:12:15
【问题描述】:
我在 Heroku 上从本地开发到生产时遇到问题。除了我在“留言簿”控制器中的一个“新”操作外,一切都运行良好。当我尝试导航到 https://allbugsaside.herokuapp.com/guestbooks/new 时,我收到一条错误消息,告诉我查看日志。当我运行 Heroku 日志时,它并没有告诉我太多信息,只是发起了一个请求。
我感觉我可能遇到了问题,因为在我的“新”操作中,我在当前没有任何数据的留言簿上调用 .all?只是一个理论。
谁能帮助解决这个问题?谢谢。
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
【问题讨论】:
-
索引在您执行
resources :guestbooks, only: [:new, :create, :index]时可用,并且根据Rails 约定,您将通过url 访问索引:/guestbooks或在任何视图内的link_to代码中:guestbooks_path。 -
我明白你在说什么。很好地抓住了代码。但没有解决问题。
标签: ruby-on-rails heroku routing