加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 模块未在控制器中加载(undefined_method错误)

发布时间:2020-12-17 02:09:21 所属栏目:百科 来源:网络整理
导读:在我的视图中应用BeforeOrAfter模块之前,尝试让它在控制台中工作.得到NoM??ethodError:未定义的methodbefore_or_after’为#`错误: s = Artist.find(1) s.events.each do |event| before_or_after(event.date) end 我选择创建一个新的modules子目录app / mo
在我的视图中应用BeforeOrAfter模块之前,尝试让它在控制台中工作.得到NoM??ethodError:未定义的methodbefore_or_after’为#`错误:

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

我选择创建一个新的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

将它包含在application_controller.rb中,因为我的所有控制器都将使用它:

class ApplicationController < ActionController::Base
  include BeforeOrAfter

解决方法

要在rails应用程序中运行,请在config / application.rb中添加以下行,

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

要在控制台中运行,请首先包含您的模块,

include BeforeOrAfter

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读