rails 4 use application helpers inside initializers

前端 未结 2 414
猫巷女王i
猫巷女王i 2021-01-21 10:37

Is it possible to include / use Application Helper methods inside of an config/initializers/browser_blocker.rb?

I am using the browser gem to detect and b

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 11:13

    Ofcourse you can but that's bad idea, i agree with that that logic supposed to somewhere in app but sometime you have to deal with it. I am saying this because you can block the request before old_browser request gets it and load rails stack.

    Anyway, this is how can you can do it

    Rails.configuration.middleware.use Browser::Middleware do
      self.class.send(:include,ApplicationHelper)
       redirect_to :controller => 'error', :action => 'browser-upgrade-required' unless    browser_is_supported?
    end
    

提交回复
热议问题