从 Rails 2 迁移到 Rails 3 问题(nil.any?)

发布于 2024-09-26 18:58:42 字数 2803 浏览 7 评论 0原文

我一直在将我的应用程序从 Rails 2.3.8 升级到 Rails 3。

完成所有基本内容(配置、路由和新的 AR API,用 Devise 替换 Authlogic)并让一些大部分静态页面正常显示后,我我网站的其余部分出现错误:

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

附加信息:

I've been upgrading my app from Rails 2.3.8 to Rails 3.

After getting all the basic stuff done (configs, routes, and the new AR API, replacing Authlogic with Devise) and getting some mostly static pages to show up fine I get an error in the rest of my site:

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?

This error always appears when I reference SomeModel for the first time.
E.g., in my news#index:

@news_story = NewsStory.new # gets called from before_filter

The full stack trace is below.
The trace seems to indicate that the model in my app/models/news_story.rb does not get loaded.

I've been struggling with this the whole day. Does anyone have an idea of what is going on?

# 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'
...

Controller 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

Additional info:

  • app/models/news_story.rb
  • Gemfile
  • config/application.rb

  • Ruby version:

    ~ $ ruby -v
    ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
    
  • Creating news story from console:

    irb(main):001:0> NewsStory.new
    => #<NewsStory id: nil, title: nil, body: nil, user_id: nil, created_at: nil, updated_at: nil>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2024-10-03 18:58:43

所以,问题是我在应用程序控制器中定义的一些parent_*方法与新的ActionController方法发生了名称冲突,这在Rails 3中没有发生。

我花了很长时间才弄清楚,该死

So, the problem was that some parent_* methods I defined in my application controller had a name collision with the new ActionController methods, which did not happen in Rails 3.

Took me forever to figure out, dammit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文