3

I am running Ruby on Rails 3.1. I would like to know a practice/technique "commonly" used to set a variable (for example, status = 'loaded') in a partilal template file so to not repeat the variable setting any time that that partial template is rendered. That is, I have a partial template file that is rendered a lot of times in the same request and I would like to display a warning message in the log only one time (that is, a single time) even if the partial template is loaded more than once... for performance reasons (or for "perfectionism"...).

In few words, I would like to display only one time a "Warning" message in the log file.

How can I (easily) do that?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Backo
  • 18,291
  • 27
  • 103
  • 170
  • I'm not sure what you're asking here, first you say you want to "to set a variable…so to not repeat the variable setting", then that you want to "display a warning message in the log only one time". These are two different things, which are you asking? (Also I'm not really sure this is a very common thing to do regardless.) – Andrew Marshall Mar 11 '12 at 05:42

1 Answers1

0

Yes, it's possible to disable logging for some messages in you log, but this approach isn't very easy (see this question) and even harmful in your case. You should ask yourself, WHY do you want this? Probably you are doing something wrong. For example, if you render the template multiple times in loop, there is special option for that, which does it with better performance! Consider you have smth like this:

<% @followers.each do |follower| %>
  <%= render :partial => "follower", :locals => {:follower => follower}  %>
<% end %>

This could be done with option :collection :

<%= render :partial => "follower", :collection => @followers %>

And it's more readable and more efficient. Full log output will help you with debugging.

Community
  • 1
  • 1
icem
  • 707
  • 7
  • 12