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

后端 未结 6 447
一生所求
一生所求 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条回答
  •  萌比男神i
    2021-02-01 07:29

    You do not have access to the request object in your models, you will have to pass the request.request_uri in.

    Perhaps via a custom method. e.g. @object.custom_method_call(params, request.request_uri)

    Another option would be add an attr_accessor :request_uri in your model and set/pass that in:

    @object.update_attributes(params.merge(:request_uri => request.request_uri))
    

提交回复
热议问题