我希望将模块MultipartForm
包含到ApplicationController
,以使用此模块中的方法扩展所有控制器的某些功能。
每次我在app中的任何控制器中进行更改时都会出现此错误。所以我必须重启我的应用程序。然后它工作正常。
来自development.log
ActionController::RoutingError (uninitialized constant ApplicationController::MultipartForm):
app/controllers/application_controller.rb:8:in `<class:ApplicationController>'
app/controllers/application_controller.rb:3:in `<top (required)>'
app/controllers/artists_controller.rb:1:in `<top (required)>'
我的application_controller.rb
是:
require_relative '../../lib/multipart_form'
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
include MultipartForm
private
def default_serializer_options
{root: false}
end
end
我的multipart_form.rb
是:
module MultipartForm
# Use in controller like: send_multipart_form(:artist => artist_params)
def send_multipart_form(send_params)
_method = params && params[:_method] ? params[:_method].to_sym : :post
RestClient.send _method, ActiveResource::Base.site.to_s + related_url, send_params
end
private
def related_url
_controller_path = params && params[:controller] ? params[:controller] : ""
_id = params && params[:id] ? params[:id] : ""
"#{_controller_path}/#{_id}"
end
end
您能否建议一个解决方案如何使用模块扩展控制器功能,而不需要在任何代码更改时重新启动服务器。如何以最好的轨道4方式做到这一点?
一些信息:
Rails server => WEBrick 1.3.1
Rails version => 4.0.2
Env => development
Ruby version => ruby 2.0.0 (2013-11-22) [x86_64-linux]
答案 0 :(得分:0)
解决方法是使用初始化程序。在rails load_extensions.rb
目录中创建initializers
load_extensions.rb是:
ActiveSupport.on_load(:action_controller) do
include MultipartForm
end