1

更新後、rubyアプリを起動しようとすると、エラーが発生します。

undefined method `[]' for nil:NilClass

抽出されたソース(8行目あたり):

8: <% account = Account.find_mak -%>

app/models/account.rb:68:in `find_mak'
app/views/layouts/_js_api_init.html.erb:8:in `block in _app_views_layouts__js_api_init_html_erb___274999121_65047152'
app/views/layouts/_js_api_init.html.erb:1:in `_app_views_layouts__js_api_init_html_erb___274999121_65047152'
app/views/layouts/logged_out.html.erb:17:in `_app_views_layouts_logged_out_html_erb__713588534_51277824'
app/controllers/leads_controller.rb:11:in `new'
config/initializers/newrelic_instrumentation.rb:30:in `call'
config/initializers/newrelic_instrumentation.rb:51:in `call'

しかし、8行目で何が起こったのかわかりません。どうすればこのエラーをデバッグできますか?回答のためにさらに情報が必要な場合は、コメントを書いてください。ありがとう。

  class Account

  include Mongoid::Document
  include Mongoid::Timestamps

  attr_accessible :org_name, :time_zone

  field :org_name
  ... ...

  def self.find_mak
    where( org_name: 'Mak' ).first
  end
end

他の場所でエラーが発生しました

<%= form_tag "/apps/#{Account.find_mak.api_key}/users",
               method: :post, remote: true,
               'data-type' => 'json',
               id: 'register',
               :class => 'custom register' do %>
4

1 に答える 1

2

あなたの見解のこのビット:

Account.find_mak.api_key

次の場合に表示されるエラーが発生します

 Account.find_mak

nilを返します。これは、データベースに一致するレコードが存在しない場合に返されます。

私の提案は#{Account.find_mak.api_key}、ビューで直接使用するのではなく、コントローラーの変数に割り当てて、nilをチェックできるようにすることです。

def some_action
  @account = Account.find_mak
  if @account.nil?
    # handle this case, i.e. a redirect to 404
  end
end  

次に、ビューで変数を使用して、nilにできないことを確認できます。

<%= form_tag "/apps/#{@accpunt.api_key}/users", #...
于 2012-08-06T14:56:04.833 に答える