简体   繁体   English

模块未在控制器中加载(undefined_method错误)

[英]module not loading in the controller (undefined_method error)

before applying the BeforeOrAfter module in my view, trying to get it to work in the console. 在我的视图中应用BeforeOrAfter模块之前,尝试让它在控制台中工作。 getting the NoMethodError: undefined method before_or_after' for #` error: 得到NoMethodError: undefined method before_or_after'为#`错误:

> s = Artist.find(1)
> s.events.each do |event|
>     before_or_after(event.date)
> end

I chose to create a new modules subdirectory, app/modules/before_or_after.rb: 我选择创建一个新的modules子目录app / modules / before_or_after.rb:

module BeforeOrAfter
  attr_reader :past
  attr_reader :future

  require "date"
  def initialize
    @datetime = DateTime.new
    @future = []
    @past = []
  end

  def before_or_after(datetime)
    if datetime < DateTime.now
      @past << datetime
    else
      @future << datetime
    end
  end
end

including it in application_controller.rb because all my controllers will be using it: 将它包含在application_controller.rb中,因为我的所有控制器都将使用它:

class ApplicationController < ActionController::Base
  include BeforeOrAfter

I don't think that the rails environment is eager loaded before running the console. 在运行控制台之前,我不认为rails环境是急切加载的。 If you're in the console make sure you require that file before using that method. 如果您在控制台中,请确保在使用该方法之前需要该文件。

If you are running the full Rails app then your file should be auto loaded. 如果您正在运行完整的Rails应用程序,那么您的文件应该是自动加载的。 You can read more about auto loading and the need for requires here . 您可以在此处阅读有关自动加载和需求的更多信息。

To run in your rails app, please add below line in config/application.rb, 要在rails应用程序中运行,请在config / application.rb中添加以下行,

config.autoload_paths << Rails.root.join('app/modules')

To run in console, include your module first like, 要在控制台中运行,请首先包含您的模块,

include BeforeOrAfter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails脚手架特定表的奇怪的undefined_method错误 - Rails scaffold strange undefined_method error for specific table 使用资源在form_with Rails 5.1.6上显示“ Undefined_method,您的意思是[复数]路径吗?”错误消息 - “Undefined_method, did you mean [pluralised] path?” error message on form_with Rails 5.1.6, using resources ActiveAdmin:date_range筛选器类型导致undefined_method错误(MetaSearch) - ActiveAdmin :date_range filter type causes undefined_method error (MetaSearch) Rails 4销毁action,undefined_method message_path - Rails 4 destroy action, undefined_method message_path 在Rails 4的控制器动作中包含模块的未定义方法错误 - Undefined method error for including a module in a controller action in Rails 4 使用act_as_taggable_on_steroids的undefined_method&#39;tag_count&#39; - undefined_method 'tag_count' using acts_as_taggable_on_steroids Rails 6.0.4 config.ru undefined_method load_server - Rails 6.0.4 config.ru undefined_method load_server Rails教程will_paginate会引发undefined_method total_pages - Rails tutorial will_paginate raises undefined_method total_pages 迁移错误:Gem:Module的未定义方法`cache&#39; - migrate error: undefined method `cache' for Gem:Module 未定义的方法“友好”从控制器获取错误 - undefined method `friendly' getting error from controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM