![](/img/trans.png)
[英]websocket with rails controller error: undefined method `action' for MessageController:Class
[英]Undefined method error for including a module in a controller action in Rails 4
在此問題的公認答案的結尾處是此聲明。
...在每個Rails應用程序中,app /下的所有目錄都會自動出現在autoload_paths和eager_load_paths中,這意味着在該目錄中添加目錄無需任何其他操作。
我在我的app
文件夾中添加了一個autoload
文件夾。 在其中有一個名為assemble_best.rb的文件,其中包含以下內容以對其進行測試:
# app/autoload/assemble_best.rb
module AssembleBest
def best_assembly(user_id,incl_confirms)
p "****"
p 'yo! it worked!'
end
end
在我的控制器中,我有:
best_assembly(current_user.id, true)
我得到的錯誤是:
undefined method `best_assembly'
在每種語法中,我嘗試添加before_action和require語句。 我還嘗試將文件夾放在lib文件夾中,並在application.rb中添加引用該文件的自動加載。 我沒有嘗試過任何工作。 還嘗試創建一個初始化程序。 我知道我可以將其添加到我的應用程序控制器中,但是它已經很大。 這是減小該控制器尺寸的第一步。
謝謝你的幫助。
您沒有在任何地方包含該模塊。 因此,錯誤。 在您的控制器中執行以下操作:
class MyController < ApplicationController
include AssembleBest
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.