繁体   English   中英

在Heroku上的ActionView :: Template :: Error(nil:NilClass的未定义方法“ program”)

[英]ActionView::Template::Error (undefined method `program' for nil:NilClass) on heroku

我的应用程序在本地运行,没有任何问题。 但是,一旦将其推送到Heroku,就无法列出帐户页面。

Heroku原木

2015-05-06T18:10:57.822981+00:00 app[web.1]:   Rendered layouts/_navbar.html.erb (81.2ms)
2015-05-06T18:10:57.823499+00:00 app[web.1]: Completed 200 OK in 84ms (Views: 3.4ms | ActiveRecord: 79.7ms)
2015-05-06T18:10:57.737903+00:00 app[web.1]: Started GET "/" for 173.210.102.18 at 2015-05-06 18:10:57 +0000
2015-05-06T18:10:57.740837+00:00 app[web.1]:   Rendered pages/index.html.erb within layouts/application (0.2ms)
2015-05-06T18:10:57.741590+00:00 app[web.1]:   Rendered layouts/_bootstrap.html.erb (0.0ms)
2015-05-06T18:10:57.823257+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (0.1ms)
2015-05-06T18:10:57.739651+00:00 app[web.1]: Processing by PagesController#index as HTML
2015-05-06T18:10:57.741030+00:00 app[web.1]:   Rendered layouts/_bootstrap.html.erb (0.0ms)
2015-05-06T18:10:57.827130+00:00 heroku[router]: at=info method=GET path="/" host=intakelog.herokuapp.com request_id=cc5c6773-706f-4de2-9ddc-a435206f666c fwd="173.210.102.18" dyno=web.1 connect=1ms service=91ms status=304 bytes=844
2015-05-06T18:10:58.143345+00:00 heroku[router]: at=info method=GET path="/assets/application-3d36a00ee9ab0b0ddd1f3a157f3d5e63.css" host=intakelog.herokuapp.com request_id=cb43b329-b4bf-4864-9340-7598b60df22f fwd="173.210.102.18" dyno=web.1 connect=1ms service=4ms status=304 bytes=133
2015-05-06T18:10:58.525722+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=intakelog.herokuapp.com request_id=3e0c1c4a-463d-499f-b0a6-f608ebc2880e fwd="173.210.102.18" dyno=web.1 connect=1ms service=4ms status=304 bytes=133
2015-05-06T18:11:05.501469+00:00 app[web.1]: Completed 500 Internal Server Error in 47ms
2015-05-06T18:11:05.502887+00:00 app[web.1]: 
2015-05-06T18:11:05.502891+00:00 app[web.1]:     19:         <td><%= account.last_name %></td>
2015-05-06T18:11:05.502898+00:00 app[web.1]:     24:         <td><%= link_to 'Edit', edit_account_path(account), class: "btn btn-link" %></td>
2015-05-06T18:11:05.502900+00:00 app[web.1]:     25:         <td><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-link" %></td>
2015-05-06T18:11:05.502893+00:00 app[web.1]:     21:         <td><%= account.phone %></td>
2015-05-06T18:11:05.502889+00:00 app[web.1]: ActionView::Template::Error (undefined method `program' for nil:NilClass):
2015-05-06T18:11:05.452737+00:00 app[web.1]: Started GET "/accounts" for 173.210.102.18 at 2015-05-06 18:11:05 +0000
2015-05-06T18:11:05.501366+00:00 app[web.1]:   Rendered accounts/index.html.erb within layouts/application (45.7ms)
2015-05-06T18:11:05.502892+00:00 app[web.1]:     20:         <td><%= account.address %></td>
2015-05-06T18:11:05.502902+00:00 app[web.1]:   app/views/accounts/index.html.erb:22:in `block in _app_views_accounts_index_html_erb___1462060006632552356_70044541209800'
2015-05-06T18:11:05.502896+00:00 app[web.1]:     23:         <td><%= link_to 'Show', account, class: "btn btn-link" %></td>
2015-05-06T18:11:05.502903+00:00 app[web.1]:   app/views/accounts/index.html.erb:16:in `_app_views_accounts_index_html_erb___1462060006632552356_70044541209800'
2015-05-06T18:11:05.502905+00:00 app[web.1]:   app/controllers/accounts_controller.rb:8:in `index'
2015-05-06T18:11:05.502906+00:00 app[web.1]: 
2015-05-06T18:11:05.502907+00:00 app[web.1]: 
2015-05-06T18:11:05.562895+00:00 app[web.1]: Started GET "/accounts" for 173.210.102.18 at 2015-05-06 18:11:05 +0000
2015-05-06T18:11:05.566966+00:00 app[web.1]:   Rendered accounts/index.html.erb within layouts/application (1.8ms)
2015-05-06T18:11:05.567052+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
2015-05-06T18:11:05.568568+00:00 app[web.1]: 
2015-05-06T18:11:05.568570+00:00 app[web.1]: ActionView::Template::Error (undefined method `program' for nil:NilClass):
2015-05-06T18:11:05.568572+00:00 app[web.1]:     19:         <td><%= account.last_name %></td>
2015-05-06T18:11:05.568576+00:00 app[web.1]:     22:         <td><%= account.program.program %></td>
2015-05-06T18:11:05.568577+00:00 app[web.1]:     23:         <td><%= link_to 'Show', account, class: "btn btn-link" %></td>
2015-05-06T18:11:05.568579+00:00 app[web.1]:     24:         <td><%= link_to 'Edit', edit_account_path(account), class: "btn btn-link" %></td>
2015-05-06T18:11:05.568581+00:00 app[web.1]:     25:         <td><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-link" %></td>
2015-05-06T18:11:05.568582+00:00 app[web.1]:   app/views/accounts/index.html.erb:22:in `block in _app_views_accounts_index_html_erb___1462060006632552356_70044541209800'
2015-05-06T18:11:05.568584+00:00 app[web.1]:   app/views/accounts/index.html.erb:16:in `_app_views_accounts_index_html_erb___1462060006632552356_70044541209800'
2015-05-06T18:11:05.568585+00:00 app[web.1]:   app/controllers/accounts_controller.rb:8:in `index'
2015-05-06T18:11:05.568587+00:00 app[web.1]: 
2015-05-06T18:11:05.568573+00:00 app[web.1]:     20:         <td><%= account.address %></td>
2015-05-06T18:11:05.568575+00:00 app[web.1]:     21:         <td><%= account.phone %></td>
2015-05-06T18:11:05.502895+00:00 app[web.1]:     22:         <td><%= account.program.program %></td>
2015-05-06T18:11:05.454378+00:00 app[web.1]: Processing by AccountsController#index as HTML
2015-05-06T18:11:05.564159+00:00 app[web.1]: Processing by AccountsController#index as HTML
2015-05-06T18:11:05.568588+00:00 app[web.1]: 
2015-05-06T18:11:05.509049+00:00 heroku[router]: at=info method=GET path="/accounts" host=intakelog.herokuapp.com request_id=fb22278a-99e2-4e1f-946c-272015814ae7 fwd="173.210.102.18" dyno=web.1 connect=1ms service=56ms status=500 bytes=1754
2015-05-06T18:11:05.571223+00:00 heroku[router]: at=info method=GET path="/accounts" host=intakelog.herokuapp.com request_id=9cde7b41-7ca0-450a-8072-d3ac3ad41050 fwd="173.210.102.18" dyno=web.1 connect=1ms service=10ms status=500 bytes=1754

我以为我的数据库可能不匹配,所以我通过运行在heroku上检查了版本

heroku run rake db:version
Running `rake db:version` attached to terminal... up, run.9594
Current version: 20150506165923

它与我的架构文件匹配。 ActiveRecord::Schema.define(version: 20150506165923)

我怀疑这与我的accounts_controller.rb文件有关。 但是,由于它在我的本地环境中运行,因此我不确定它可能是什么。

accounts_controller.rb

class AccountsController < ApplicationController
  before_action :set_account, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @account = Account.all
    respond_with(@account)
  end

  def show
    @notes = Note.where(account_id: @account.id) #Where a note belong to the current account
  end

  def new
    @account = Account.new
    respond_with(@account)
  end

  def edit
  end

  def create
    @account = Account.new(account_params)
    @account.save
    respond_with(@account)
  end

  def update
    @account.update(account_params)
    respond_with(@account)
  end

  def destroy
    @account.destroy
    respond_with(@account)
  end

  private
    def set_account
      @account = Account.find(params[:id])
    end

    def account_params
      params.require(:account).permit(:first_name, :last_name, :program_id, :address, :phone)
    end
end

用于帐户的index.html.erb

<h1>Listing accounts</h1>

<table class="table table-hover table-condensed">
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Address</th>
      <th>Phone</th>
      <th>Program</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @account.each do |account| %>
      <tr>
        <td><%= account.first_name %></td>
        <td><%= account.last_name %></td>
        <td><%= account.address %></td>
        <td><%= account.phone %></td>
        <td><%= account.program.program %></td>
        <td><%= link_to 'Show', account, class: "btn btn-link" %></td>
        <td><%= link_to 'Edit', edit_account_path(account), class: "btn btn-link" %></td>
        <td><%= link_to 'Destroy', account, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-link" %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Account', new_account_path %>

耙道

rake routes
                  Prefix Verb   URI Pattern                                    Controller#Action
        new_user_session GET    /users/sign_in(.:format)                       devise/sessions#new
            user_session POST   /users/sign_in(.:format)                       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                      devise/sessions#destroy
           user_password POST   /users/password(.:format)                      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                 devise/passwords#edit
                         PATCH  /users/password(.:format)                      devise/passwords#update
                         PUT    /users/password(.:format)                      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                        devise/registrations#cancel
       user_registration POST   /users(.:format)                               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)                       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                          devise/registrations#edit
                         PATCH  /users(.:format)                               devise/registrations#update
                         PUT    /users(.:format)                               devise/registrations#update
                         DELETE /users(.:format)                               devise/registrations#destroy
           account_notes POST   /accounts/:account_id/notes(.:format)          notes#create
        new_account_note GET    /accounts/:account_id/notes/new(.:format)      notes#new
       edit_account_note GET    /accounts/:account_id/notes/:id/edit(.:format) notes#edit
            account_note PATCH  /accounts/:account_id/notes/:id(.:format)      notes#update
                         PUT    /accounts/:account_id/notes/:id(.:format)      notes#update
                         DELETE /accounts/:account_id/notes/:id(.:format)      notes#destroy
                accounts GET    /accounts(.:format)                            accounts#index
                         POST   /accounts(.:format)                            accounts#create
             new_account GET    /accounts/new(.:format)                        accounts#new
            edit_account GET    /accounts/:id/edit(.:format)                   accounts#edit
                 account GET    /accounts/:id(.:format)                        accounts#show
                         PATCH  /accounts/:id(.:format)                        accounts#update
                         PUT    /accounts/:id(.:format)                        accounts#update
                         DELETE /accounts/:id(.:format)                        accounts#destroy
                programs GET    /programs(.:format)                            programs#index
                         POST   /programs(.:format)                            programs#create
             new_program GET    /programs/new(.:format)                        programs#new
            edit_program GET    /programs/:id/edit(.:format)                   programs#edit
                 program GET    /programs/:id(.:format)                        programs#show
                         PATCH  /programs/:id(.:format)                        programs#update
                         PUT    /programs/:id(.:format)                        programs#update
                         DELETE /programs/:id(.:format)                        programs#destroy
             pages_index GET    /pages/index(.:format)                         pages#index
                    root GET    /                                              pages#index

尝试这个:

代替:

<td><%= account.program.program %></td>

尝试:

<td><%= account.program ? account.program.program : "" %></td>

在尝试访问account.program的任何属性之前,您需要验证该account.program是否存在,以下是代码的缩写形式:

<td>

<%if account.program%>
   <%=account.program.program%>
<%else%>
   Program doesn't exist
<%end%>

</td>

看来您尚未在帐户控制器中定义任何称为program的方法。

<td><%= account.program.program %></td>

这似乎是引发该错误。 尝试注释掉此代码并重新部署。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM