尝试在控制器中加载服务时Rails错误(Rails error while trying to load a Service in a controller)

编程入门 行业动态 更新时间:2024-10-10 03:27:19
尝试在控制器中加载服务时Rails错误(Rails error while trying to load a Service in a controller)

我的控制器使用create方法定义如下。

class AtestController < BaseController def create result = create_something(params) @connection = Bunny.new @connection.start @channel = @connection.create_channel bunny = RabbitPublisherService::RabbitPublisher.new(@channel,@connection) render :json => trigger, :status => :created end end

我的Rabbit Publisher服务定义如下

module RabbitPublisherService class RabbitPublisher private attr_accessor :channel, :connection def initialize(channel, connection) puts "I reached here" @channel = channel @connection = connection end def publish(message) q = @channel.queue("task_queue", :durable => true) q.publish(message, :persistent => true) puts "Message is Published..." sleep 1.0 @connection.close end end end

当我尝试从控制器创建方法,RabbitPublisherService :: RabbitPublisher.new中调用此服务时,我得到一个未初始化的常量错误:error_message =>“uninitialized constant AtestController :: RabbitPublisherService”

有人可以帮我找出我做错了什么吗?

My controller is defined as follows with a create method.

class AtestController < BaseController def create result = create_something(params) @connection = Bunny.new @connection.start @channel = @connection.create_channel bunny = RabbitPublisherService::RabbitPublisher.new(@channel,@connection) render :json => trigger, :status => :created end end

My Rabbit Publisher Service is defined as follows

module RabbitPublisherService class RabbitPublisher private attr_accessor :channel, :connection def initialize(channel, connection) puts "I reached here" @channel = channel @connection = connection end def publish(message) q = @channel.queue("task_queue", :durable => true) q.publish(message, :persistent => true) puts "Message is Published..." sleep 1.0 @connection.close end end end

When I try calling this service from the controllers create method, RabbitPublisherService::RabbitPublisher.new, I get an uninitialized constant error saying :error_message=>"uninitialized constant AtestController::RabbitPublisherService"

Can someone please help me find out what I am doing wrong?

最满意答案

通常,这可能是您放置文件的位置以及Rails期望它们的位置的问题。 可以在此处找到深入的指南: http : //guides.rubyonrails.org/autoloading_and_reloading_constants.html 。

要解决它,有三种选择:

把模块和类放在Rails自动加载期望的地方(IMO这是首选的解决方案) 要求定义类的文件 将包含该类的文件所在的文件夹添加到自动加载路径

1:您需要确保RabbitPublisherService和RabbitPublisher位于Rails可以自动加载它们的位置,例如app/services/rabbit_publisher_service.rb和app/services/rabbit_publisher_service/rabbit_publisher.rb 。

2:使用require或(可能更好) require_relative 。

3:或者,您可以显式添加自动加载/预加载路径的路径。

在我看来,你应该坚持使用选项1,除非你有充分的理由不这样做。 使用Rails的默认值可以将代码保持在最低限度,并且可以防止期望Rails默认值的开发人员出现意外情况。 数字2也可以,因为它是明确的。 我肯定会避免使用选项3,因为当你将大量文件放在意想不到的位置时,它迟早会造成混乱,这使得习惯Rails默认并期望它们的其他开发人员更难。

Generally, this is probably a problem with where you've put your files and where Rails expect them. An in-depth guide can be found here: http://guides.rubyonrails.org/autoloading_and_reloading_constants.html.

To solve it, there are three options:

put the module and class where Rails autoloading expects them (IMO this is the preferred solution) require the file where the class is defined add the folder where the file containing the class is located to the autoload paths

1: You need to make sure that RabbitPublisherService and RabbitPublisher are in a location that Rails can autoload them, for instance app/services/rabbit_publisher_service.rb and app/services/rabbit_publisher_service/rabbit_publisher.rb.

2: Use require or (probably better) require_relative.

3: Alternatively you can explicitly add the path to the autoload/eager load paths.

In my opinion, you should stick with option 1 unless you have a very good reason not to. Using Rails' defaults keeps the code to a minimum and prevents surprises for fellow developers who expect Rails defaults. Number 2 is OK as well since it's explicit. I'd definitely avoid option 3 because it sooner or later always creates a mess when you put loads of files in unexpected locations and it makes it harder for other developers who are used to Rails defaults and expect them.

更多推荐

本文发布于:2023-07-27 08:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   加载   错误   Service   controller

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!