2015-12-06 27 views
0

我在從Heroku的本地開發到生產方面遇到問題。除了我在'留言簿'控制器中的一個'新'動作外,一切正常。當我嘗試導航到https://allbugsaside.herokuapp.com/guestbooks/new時,出現錯誤提示我要查看日誌。當我運行Heroku日誌時,除了發起請求之外,它並沒有告訴我很多。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

當您執行'resources:guestbooks,只有:[:new,:create,:index]'時,索引是可用的,通過Rails約定,您可以通過url:'/ guestbooks'或link_to '代碼在任何視圖:'guestbooks_path'。 –

+0

我明白你的意思了。良好的代碼捕獲。雖然沒有解決問題。 – Jbur43

回答

0

你做了heroku run rake db:migrate爲了讓你的數據庫更新?當然,只有當你在你的本地環境中進行新的遷移。如果這不能解決您的問題,請發佈來自heroku的日誌。

請記住,Rails慣例使用index(在您的控制器中查看和定義)來顯示您從某個模型中需要的所有對象。

在這裏,您想要在new的對象視圖中顯示它們。這是可能的,只是要小心。你的看法叫做new.html.erb

另外請記住,def new不是一個動作,它只是一個控制器,爲您的new.html.erb提供數據,變量,重定向和其他東西到稱爲so的視圖。

+0

我添加了我的new.html.erb模板並將文件路由到此問題。我運行了heroku運行rake db:migrate,無濟於事。 @JuanM。 – Jbur43

+0

我不熟悉簡單的表單,但試試這個:'<%= simple_form_for @guestbook do | f | %>'沒有更多的參數。在Rails 4中,沒有必要指定其他的東西,因爲@guestbook是一個新的實例,提交按鈕將引導行動到創建方法。 –

0

我注意到您的頁面/留言簿不響應索引操作。你能發佈的routes.rb

Ruby是動態的,所以技術上:

@guestbooks = Guestbook.all 
@guestbooks = Kaminari.paginate_array(@guestbooks).page(params[:page]).per(5) 

但我不會去做。重新定義一個變量並將其用作對聲明的引用,與這句話一樣難以理解。

@guestbooks = Kaminari.paginate_array(Guestbook.all).page(params[:page]).per(5) 

如果問題存在於生產中,而不是在dev中,則它不會在發佈代碼時出現問題。這在數據庫中缺少數據不是問題,但是它可能是模式的問題。