Will not be useful functionality similar https://github.com/shell/rails3_before_render ? Something like ActionController filters but not before action. Filters before render. See also http://www.perfectline.ee/blog/ruby-on-rails-before-render-filter, http://penkin.co.uk/rails3_before_render-plugin/. And don’t you know gem for this? Thanks.
Please show any real use case where this will be useful, to make things more clear. Otherwise it’s just hard to figure it out examples where it’s useful or not. Thanks.
I think that links’ authors know better than me all of causes. But I use render filters for decorating objects (similar to https://github.com/ohwillie/decorates_before_rendering) and setting page variables (that procedure is equal for each controller and use result of action’s action).
P.S. It will be simply to realize with accepting suggestion Redirecting to Google Groups .
This explanation seems almost schizophrenic.
What are you trying to say? Could you slow down and explain it in more elaborate terms please?
I guess he wants some mechanism that hooks into the action processing workflow so you can declarativly define stuff (in the controller, like a filter) that is done with the model before it is passed to the rendering layer. Just guessing without following the links!
Hi,
Reviving an old topic about the need of a before_render filter (topic found through Google, my apologies if there is a more recent thread)
Here is a use case:
- A controller’s parent class is preparing a set of JavaScript variables to be passed to the view
- The view template is inserting those JavaScript variables in the HTML code
- The JavaScript variables need elements loaded/set by the controller
As the controller action takes place after the action filters and before the rendering, the only actual solution is to patch render
.
Code example: class ApplicationController
def render(*args)
unless @we_already_prepared_js_vars
@we_already_prepared
_js_vars = true # This is to avoid loading those variable for each call of render
@_js_vars = prepare_js_vars
end
super
end end
class UserController < `ApplicationController before_filter :load_user
def ``prepare_js_vars` {record_created_at: @user.created_at} end
def load_user @user = User.find params[:id] end end
``
Would be more elegant to use a before_render filter in the ApplicationController
Regards
Hi Benjamin,
This isn’t really a valid use case. The way most of us would tackle something like this is to write a helper that lazy-loads the value in question, and call this from the view. This helper can exist in the controller class, if it needs access to your controller methods, and be made available to the view with the helper_method macro.
-Ernie
Hi Emie,
Thanks for the quick answer.
This is actually what I was implementing while searching for an alternate solution, your answer confirm I’m on the right track.
Regards,
Benjamin