1

Basically, I have a method that I want to use upon multiple controllers.

def has_been_updated_today
  # Code that checks if a Model has been updated...
end

In this blog the author stated that it should be placed in the /modules directory, and then you can simply include in any file.

The problem I have with this is that everything related to my app is in the app directory. Everything about configuring intial set up is in the config just like everything about the database is in the db.

Furthermore, I am still get re-familiar with Rails, but how does include filename know where to get filename.rb from? How does it know it's in lib when I never stated it?

dsomel21
  • 576
  • 2
  • 8
  • 27

1 Answers1

1

In a comment you write that you want to use it in a few controllers and concerns seem the better solution for you problem.

# app/controllers/clock_controller.rb
class ClockUpdated
  include ClockConcern
  before_filter :has_been_updated_today, only: [:show] # just a example

  def update
    if !has_been_updated_today
      flash[:error] = 'Not been updated'
    end
  end
end

# app/controllers/concerns/clock_updated.rb
module ClockUpdated
  extend ActiveSupport::Concern
  def has_been_updated_today
    return true
  end
end
Thomas R. Koll
  • 3,131
  • 1
  • 19
  • 26
  • As of right now, my code looks very similar to yours, besides the fact it's in `/lib`. I am going to research what `concerns` are for before... I make this change. Thank you for sharing this knowledge with me! – dsomel21 Apr 27 '17 at 16:37
  • Did you mean `include ClockUpdated`? – dsomel21 Apr 27 '17 at 19:34