アプリをRails2.3.8からRails3にアップグレードしています。
すべての基本的な作業(構成、ルート、新しいAR API、AuthlogicをDeviseに置き換える)を完了し、ほとんど静的なページを正常に表示した後、サイトの残りの部分でエラーが発生します。
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.any?
SomeModel
このエラーは、初めて参照するときに常に表示されます。例:私のnews#index
:
@news_story = NewsStory.new # gets called from before_filter
完全なスタックトレースは以下のとおりです。トレースは、私のモデルapp/models/news_story.rb
がロードされていないことを示しているようです。
私は一日中これに苦労してきました。誰かが何が起こっているのか考えていますか?
# stack trace:
activesupport (3.0.0) lib/active_support/dependencies.rb:497:in `load_missing_constant'
activesupport (3.0.0) lib/active_support/dependencies.rb:183:in `block in const_missing'
activesupport (3.0.0) lib/active_support/dependencies.rb:181:in `each'
activesupport (3.0.0) lib/active_support/dependencies.rb:181:in `const_missing'
app/controllers/news_controller.rb:82:in `new_post'
activesupport (3.0.0) lib/active_support/callbacks.rb:451:in `_run__810448074__process_action__85542351__callbacks'
activesupport (3.0.0) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.0) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
...
コントローラのアクション:
class NewsController < ApplicationController
before_filter :new_post_from_params, :only => :create
before_filter :new_post, :only => [:index, :new]
before_filter :find_post, :except => [:index, :create, :new]
filter_access_to :all, :attribute_check => true
def index
@user = current_user
@news = NewsStory.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @user.news }
end
end
#...
private
def new_post_from_params
@news_story = NewsStory.new(params[:news_story])
end
def new_post
@news_story = NewsStory.new
end
def find_post
@news_story = NewsStory.find(params[:id])
end
end
追加情報:
- app / models / news_story.rb
- Gemfile
Rubyバージョン:
~ $ ruby -v ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
コンソールからのニュース記事の作成:
irb(main):001:0> NewsStory.new => #<NewsStory id: nil, title: nil, body: nil, user_id: nil, created_at: nil, updated_at: nil>