ruby on rails - How to set in a middleware a variable accessible in all my application? -
i using ruby on rails 3 , trying use middlewares in order set variable @variable_name
accessible later in controllers.
for example middleware is
class auth def initialize(app) @app = app end def call(env) @account ||= account.find(1) @app.call(env) end end
the above code set @account
variable, isn't available in application (in controllers, models, views, ...). so, how can accomplish that?
i seen this answer way need, have @account
variable "directly accessible". is, without use way making available, example in views, this:
<%= debug @account %>
you can use 'env' that. in middleware this:
def call(env) env['account'] = account.find(1) @app.call(env) end
you can value using 'request' in app:
request.env['account']
and please don't use global variables or class attributes people suggest here. that's sure way troubles , bad habit.
Comments
Post a Comment