【问题标题】:Migrating from Rails 2 to Rails 3 problem (nil.any?)从 Rails 2 迁移到 Rails 3 的问题(nil.any?)
【发布时间】:2010-10-09 22:33:14
【问题描述】:

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

完成所有基本工作(配置、路由和新的 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
  • config/application.rb

  • 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>
    

【问题讨论】:

  • 您能否发布您的控制器操作,特别是在第 82 行附近?
  • 完成。第 82 行是 @news_story = NewsStory.new in #new_post
  • 这个错误应该没关系,但是为什么索引操作需要一个新的NewsStory?
  • 哦,如果您是管理员,您可以从同一屏幕发布新故事(为方便起见)
  • 这看起来不像是找不到类时会出现的错误。 NewsStory 的初始化程序中是否包含任何内容?

标签: ruby-on-rails activerecord ruby-on-rails-3 actioncontroller


【解决方案1】:

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

我花了很长时间才弄明白,该死的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多