2014-01-18 48 views
0

如果我正在讨论这个错误,请让我知道我可以改变它。我在config/initializers/payload_signer.rb中有一个文件。我正在试图在名为device_enrollment_controller.rb的控制器中使用此文件。Ruby on rails从控制器访问文件

PayloadSigner.sign(get_profile) 

get_profile是获取我需要的文件并返回它的控制器中的方法。 PayloadSigner引用其他文件。当我尝试运行此操作时(记住im确实必须在payload_signer中进行更改,因为它正常工作),我得到的错误是未初始化的常量DeviceEnrollmentController :: PayloadSigner。这导致我相信我正确地引用了payload_signer.rb文件。我已经尝试过像include和load这样的东西,但到目前为止它们都不起作用。

任何帮助或指导表示赞赏。

回答

1

Rails的初始化器控制器模式被调用。所以它不会工作。初始化器不适用于这种用途。相反,我建议将您的代码放入控制器before_filter。无论是在ApplicationController还是仅在那些需要它的控制器中(例如DeviceEnrollmentController)。事情是这样的:

class DeviceEnrollmentController # Or ApplicationController 

    before_filter :sign_payload 

    protected 

    def get_profile 
    # Magic 
    end 

    def sign_payload 
    PayloadSigner.sign(get_profile) 
    end 
end 

编辑:又如:

class DeviceEnrollmentController 

    # The filter is only applied to the sign action 
    # (that's what the :only parameter does). 
    before_filter :sign_payload, :only => [:sign] 

    # Browsing to /show, you render this magic button of yours. 
    def show 
    # Render page that holds the button 
    end 

    # The magic button is bound to the /sign route. 
    # Clicking on the button calls this action. 
    def sign 
    # When you get here, the #sign_payload method 
    # has already been called. 
    end 

    protected 

    def get_profile 
    # Magic 
    end 

    def sign_payload 
    PayloadSigner.sign(get_profile) 
    end 
end 
+0

想如果我告诉你,访问此控制器的特定页面必须加载,然后你点击这个仍然成立按钮。这是什么让你签署的领域。 – Brandon

+0

检查我的另一个例子,并让我知道,如果这是你的想法。 – lipanski