before applying the BeforeOrAfter
module in my view, trying to get it to work in the console. getting the NoMethodError: undefined method
before_or_after' for #` error:
> 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:
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:
class ApplicationController < ActionController::Base
include BeforeOrAfter
I don't think that the rails environment is eager loaded before running the console. 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. 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,
config.autoload_paths << Rails.root.join('app/modules')
To run in console, include your module first like,
include BeforeOrAfter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.