gpt4 book ai didi

ruby-on-rails - 是否可以在许多不同的 Controller 中定义 ActionController::Base?

转载 作者:数据小太阳 更新时间:2023-10-29 08:38:00 26 4
gpt4 key购买 nike

如果您有一个 OrdersController,您可以这样定义类吗?

class OrdersController < ActionController::Base
# stuff
end

当我生成一个 Rails 脚手架时,唯一似乎定义的地方是 ApplicationController(application_controller.rb),我创建的其他 Controller 有类似

class OrdersController < ApplicationController
# stuff
end

使用第一个是否正确?在这种情况下?我似乎找不到一个好的答案。

最佳答案

这在技术上是可行的,但不建议这样做。

阅读:http://api.rubyonrails.org/classes/ActionController/Base.html

引自页面:

By default, only the ApplicationController in a Rails application inherits from ActionController::Base. All other controllers in turn inherit from ApplicationController. This gives you one class to configure things such as request forgery protection and filtering of sensitive request parameters.

您也可以直接从 ActionController::Base 继承。这只是类继承的一个例子。这里的 ActionController::Base 更像是一个抽象类。

关于ruby-on-rails - 是否可以在许多不同的 Controller 中定义 ActionController::Base?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23210174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com