无法找到行动

时间:2015-12-03 21:29:26

标签: ruby-on-rails ruby-on-rails-4

道歉的标题道歉。我正在关注https://www.railstutorial.org/book/beginning#cha-beginning教程,以了解cloud9上的RoR。我在控制器中进行了更改,如第1章标题1.4

中所列
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  def hello
    render text: "hello, world!"
  end
end

和我的routes.rb文件

Rails.application.routes.draw do
  root 'application#hello'
end

但是当我重新启动服务器时,我得到了错误

Started GET "/" for 69.157.46.139 at 2015-12-03 21:20:46 +0000

AbstractController::ActionNotFound (The action 'hello' could not be found for ApplicationController):
  actionpack (4.2.2) lib/abstract_controller/base.rb:132:in `process'
  actionview (4.2.2) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.2) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.2) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:74:in `call'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:43:in `serve'
  actionpack (4.2.2) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:819:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.2) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
  activerecord (4.2.2) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.2) lib/active_support/callbacks.rb:88:in `call'
  activesupport (4.2.2) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.2) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
  activesupport (4.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
  web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.2) lib/rails/engine.rb:518:in `call'
  railties (4.2.2) lib/rails/application.rb:164:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/unknown_action.html.erb within rescues/layout (9.6ms)


Started GET "/" for 69.157.46.139 at 2015-12-03 21:21:46 +0000

AbstractController::ActionNotFound (The action 'hello' could not be found for ApplicationController):
  actionpack (4.2.2) lib/abstract_controller/base.rb:132:in `process'
  actionview (4.2.2) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.2) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.2) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:74:in `call'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:43:in `serve'
  actionpack (4.2.2) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.2) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.2) lib/action_dispatch/routing/route_set.rb:819:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.2) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:649:in `call'
  activerecord (4.2.2) lib/active_record/migration.rb:378:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.2) lib/active_support/callbacks.rb:88:in `call'
  activesupport (4.2.2) lib/active_support/callbacks.rb:88:in `_run_callbacks'
  activesupport (4.2.2) lib/active_support/callbacks.rb:776:in `_run_call_callbacks'
  activesupport (4.2.2) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:18:in `middleware_call'
  web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.2) lib/rails/engine.rb:518:in `call'
  railties (4.2.2) lib/rails/application.rb:164:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates/rescues/unknown_action.html.erb within rescues/layout (0.6ms)

我已经尝试了root to: 'application#hello',但仍然无法正常工作

2 个答案:

答案 0 :(得分:0)

应用程序控制器不用于定义操作。这是基本控制器,其他控制器可能从中继承行为/属性。你应该创建另一个控制器并在那里定义你好。

使用生成器

rails g controller Welcome hello

路由文件

root Welcome#hello

get 'hello' => 'welcome#hello'

应用程序/控制器/ welcome_controller.rb

class WelcomeController < ApplicationController
  def hello

  end
end

答案 1 :(得分:0)

谢谢大家。问题出在Cloud9 IDE上。我在创建application_controller方法之后保存了hello,但IDE没有保存它,直到我明确点击File > Save。我在cat application_Controller.rb时发现它,并注意到那里不存在hello方法。