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

On Wednesday, June 17, 2015 at 6:57:14 AM UTC-4, Benjamin B wrote:
>
> 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
>
>
>
> On Friday, September 7, 2012 at 8:58:17 PM UTC+2, Carlos Antonio da Silva 
> wrote:
>>
>> 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.
>>
>> On Wed, Sep 5, 2012 at 3:53 PM, Alexander Kurakin <[email protected]> 
>> wrote:
>>
>>> 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.
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Ruby on Rails: Core" group.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msg/rubyonrails-core/-/VVF2HksqidUJ.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to 
>>> [email protected].
>>> For more options, visit this group at 
>>> http://groups.google.com/group/rubyonrails-core?hl=en.
>>>
>>
>>
>>
>> -- 
>> At.
>> Carlos Antonio
>>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to