【问题标题】:Action defined in ApplicationController can not be found找不到 ApplicationController 中定义的操作
【发布时间】:2016-01-12 04:20:32
【问题描述】:

我正在创建一个 Rails 4.2 应用程序以在网页上打印“Hello World”。但是我在运行页面时得到的是这个错误:

未知动作 找不到动作“你好” 应用控制器

这些是我更新的文件:

application_controller.rb

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
  # You can have the root of your site routed with "root"
  root 'application#hello'
end

【问题讨论】:

  • 您将从这个问题中得到答案。 stackoverflow.com/questions/4778301/…
  • 通常你在尾部定义一个新的控制器为welcome控制器。然后你在route.rb 中定义root :to =&gt; "welcome#index"

标签: ruby-on-rails ruby-on-rails-4 railstutorial.org


【解决方案1】:

如果您使用的是 cloud9 并按照 www.railstutorial.org 上的教程进行操作。那么你可能有正确的代码。您只需按下开发环境顶部中心的“运行”按钮。提示,它是绿色的。

本教程中省略了此细节,但从外观上看,您在 ide 中所做的编辑在您单击“运行”之前不会保存。似乎一个人自己清除的门槛很低,但我花了几个小时的谷歌和自我怀疑。所以试试吧。

【讨论】:

    【解决方案2】:

    这样使用。

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

    【讨论】:

      【解决方案3】:

      您的根路由配置正确,请准确检查控制器和操作名称是否为ApplicationControllerhello

      但是ApplicationController 被设计为基本控制器,你应该在这里放置通用逻辑,并从中扩展你的控制器。

      如果您使用生成器创建控制器,它将自动完成。

      您可以在official docs找到更多信息。

      【讨论】:

        【解决方案4】:

        我认为问题在于您正在调用render text: ... IE,您不允许Rails 完成其渲染过程。如果有错,我很乐意删除答案;错误似乎太简单,无法基于语法。

        1. 重启服务器
        2. 这样做:

        -

        #config/routes.rb
        root "application#index"
        
        #app/controllers/application_controller.rb
        class ApplicationController < ActionController::Base
        end
        
        #app/views/application/index.html.erb
        Hello World
        

        您可以从这个答案中了解your controller doesn't need methods 如何仅加载视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-25
          相关资源
          最近更新 更多