尝试从Rails ApplicationContoller渲染另一个控制器的视图时出现问题?

时间:2011-11-24 13:52:01

标签: ruby-on-rails ruby

我正在尝试使用ApplicationController来捕获异常(在应用程序的较低级别抛出),然后通过ErrorController呈现一个简单的错误页面。但是,我似乎无法在ApplicationController中使用'render'。这是代码:

# myapp/app/controllers/application_controller.rb
-----------------------------------------------
class ApplicationController < ActionController::Base
  protect_from_forgery

  rescue_from MyError, :with => :handle_my_error

  public

  private

  def handle_my_error
    render 'error/show'
  end
end

# myapp/spec/controllers/application_controller_spec.rb
-------------------------------------------------------
require "spec_helper"

describe ApplicationController do
  controller do
    def index
        raise MyError, "Dummy Message"
      end
    end
  end

  describe "when a MyError occurs" do
    it "should redirect to the error page" do
      get :index
      # TODO - assertion should_render to go here
    end
  end
end

# myapp/app/controllers/error_controller.rb
-------------------------------------------
class ErrorController < ApplicationController
  def show
  end
end

# myapp/app/views/error/show.html.haml
%h1 ERROR

因此,正如您所看到的,我在我的规范中使用匿名控制器来测试此功能。当我从IntelliJ / RubyMine运行测试时,我收到以下错误:

/home/me/.rvm/rubies/jruby-1.6.4/bin/jruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/me/.rvm/gems/jruby-1.6.4@myapp/bin/rspec /home/me/dev/myapp/spec/controllers/application_controller_spec.rb --require teamcity/spec/runner/formatter/teamcity/formatter --format Spec::Runner::Formatter::TeamcityFormatter
Testing started at 1:39 PM ...
No DRb server is running. Running in local process instead ...

    TypeError: can't convert nil into String
org/jruby/RubyString.java:2412:in `concat'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/template/resolver.rb:16:in `build'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/template/resolver.rb:127:in `find_templates'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/template/resolver.rb:45:in `find_all'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/template/resolver.rb:74:in `cached'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/template/resolver.rb:44:in `find_all'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/path_set.rb:21:in `find_all'
org/jruby/RubyArray.java:1603:in `each'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/path_set.rb:20:in `find_all'
org/jruby/RubyArray.java:1603:in `each'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/path_set.rb:19:in `find_all'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-rails-2.7.0/lib/rspec/rails/view_rendering.rb:60:in `find_all'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/path_set.rb:21:in `find_all'
org/jruby/RubyArray.java:1603:in `each'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/path_set.rb:20:in `find_all'
org/jruby/RubyArray.java:1603:in `each'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/path_set.rb:19:in `find_all'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/path_set.rb:29:in `exists?'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_view/lookup_context.rb:94:in `exists?'
org/jruby/RubyKernel.java:2100:in `send'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/view_paths.rb:11:in `template_exists?'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/layouts.rb:282:in `_layout'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/layouts.rb:347:in `_default_layout'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/layouts.rb:325:in `_layout_for_option'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/layouts.rb:292:in `_normalize_options'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/rendering.rb:51:in `_normalize_options'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/streaming.rb:227:in `_normalize_options'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/compatibility.rb:38:in `_normalize_options'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/rendering.rb:146:in `_normalize_render'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/rendering.rb:98:in `render'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/rendering.rb:16:in `render'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/instrumentation.rb:40:in `render'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/activesupport-3.1.1/lib/active_support/core_ext/benchmark.rb:5:in `ms'
/home/me/.rvm/rubies/jruby-1.6.4/lib/ruby/1.8/benchmark.rb:308:in `realtime'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/activesupport-3.1.1/lib/active_support/core_ext/benchmark.rb:5:in `ms'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/instrumentation.rb:40:in `render'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/instrumentation.rb:39:in `render'
/home/me/dev/myapp/app/controllers/application_controller.rb:12:in `handle_my_error'
org/jruby/RubyMethod.java:117:in `call'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/activesupport-3.1.1/lib/active_support/rescuable.rb:80:in `rescue_with_handler'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/rescue.rb:12:in `rescue_with_handler'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/rescue.rb:19:in `process_action'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/activesupport-3.1.1/lib/active_support/notifications.rb:53:in `instrument'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/activesupport-3.1.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/activesupport-3.1.1/lib/active_support/notifications.rb:53:in `instrument'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/base.rb:121:in `process'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/abstract_controller/rendering.rb:45:in `process'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/test_case.rb:449:in `process'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/test_case.rb:49:in `process'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/actionpack-3.1.1/lib/action_controller/test_case.rb:365:in `get'
/home/me/dev/myapp/spec/controllers/application_controller_spec.rb:16:in `(root)'
org/jruby/RubyKernel.java:2061:in `instance_eval'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example.rb:51:in `run'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example.rb:110:in `with_around_hooks'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example.rb:48:in `run'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:306:in `run_examples'
org/jruby/RubyArray.java:2336:in `collect'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:302:in `run_examples'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:274:in `run'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:275:in `run'
org/jruby/RubyArray.java:2336:in `collect'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/example_group.rb:275:in `run'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:24:in `run'
org/jruby/RubyArray.java:2336:in `collect'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:24:in `run'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/reporter.rb:12:in `report'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:21:in `run'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in `run_in_process'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:66:in `run'
/home/me/.rvm/gems/jruby-1.6.4@myapp/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in `autorun'
org/jruby/RubyProc.java:274:in `call'
org/jruby/RubyProc.java:233:in `call'

1 example, 1 failure, 0 passed

Finished in 11.58 seconds


Process finished with exit code 1

我按照本指南强调如何使用'渲染': http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

当我使用'重定向'的东西工作,但我不想重定向我想直接渲染页面。有任何想法吗?我错过了Rails开发的基本内容吗?

在SO上有类似的问题,但似乎没有一个问题能解决我在这里面临的问题。

更新:我通过将JRuby的语言级别设置为1.9来解决此问题。我的IDE使用1.8,这是我遇到问题的地方。命令行很好,使用1.9。

1 个答案:

答案 0 :(得分:0)

我通过将JRuby的语言级别设置为1.9来解决了这个问题。我的IDE使用1.8,这是我遇到问题的地方。命令行很好,使用1.9。