How do i get request.uri in model in Rails?

后端 未结 6 380
一生所求
一生所求 2021-02-01 07:00
$request = request

When I write this in controller, it will work. But if i need this variable in Model or Application controller, How can i ?

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 07:25

    if you use rails > 5.0, you can do below

    add a module in models/concern

    module Current
      thread_mattr_accessor :actor
    end
    

    in applicaton_controller do

    around_action :set_thread_current_actor
    
      private
    
      def set_thread_current_actor
        Current.actor = current_user
        yield
      ensure
        # to address the thread variable leak issues in Puma/Thin webserver
        Current.actor = nil
      end
    

    then in thread anywhere get current_user

    Current.actor
    

提交回复
热议问题