Ответ 1
Создайте файл .rb в каталоге config/initializers
со следующим кодом:
Youtube::display.class_eval do
def find(id, options = {})
//Code here
end
end
Лучшая практика для переопределения метода класса gem в приложении rails?, Мне нужно переопределить поведение метода поиска драгоценного камня.
Ниже приведен код в драгоценном камне
module Youtube
class display
attr_accessor :base
def find(id, options = {})
detailed = convert_to_number(options.delete(:detailed))
options[:detailed] = detailed unless detailed.nil?
base.send :get, "/get_youtube", options.merge(:youtube_id => id)
end
end
end
Как переопределить вышеупомянутый метод поиска в моем контроллере YoutubeSearch Rails?
def find(id, options = {})
//Code here
end
Создайте файл .rb в каталоге config/initializers
со следующим кодом:
Youtube::display.class_eval do
def find(id, options = {})
//Code here
end
end
Я разработал такое решение, которое НЕ НЕ требует перезапуска сервера Rails после каждого изменения кода (в отличие от всех других решений):
1. Создать YoutubeHelper.rb
module YoutubeHelper
include Youtube
def init_youtube_helper
display.class_eval do
def find(id, options = {})
//Code here
end
end
end
end
2. youtube_search_controller.rb
class YoutubeSearchController < ActionController::Base
include YoutubeHelper
before_action :init_youtube_helper
end