Reputation: 884
I use some like this:
render partial: "shared/user/avatar_with_nickname", locals: user
I use this partial many times in views of different models and it's conveniently and it's work, but that is bad for time load.
There is console webrick:
Rendered shared/user/_avatar_with_nickname.html.haml (15.9ms) Rendered shared/user/_avatar_with_nickname.html.haml (14.8ms) Rendered shared/user/_avatar_with_nickname.html.haml (17.6ms) etc
Is there any way to use it like this and reduce rendiring time? Maybe pre-cache or some else?
Upvotes: 1
Views: 501
Reputation: 884
I have found solution! If it possible don't use rendering in a loop. That is bad idea because of rendering consume time for open/close file every iteration. But sometimes it's necessary or very comfortably to use as in my case.
So, my solution is using helper. I create my own helper avatar_for and after this rendering partial became faster a lot.
Upvotes: 0
Reputation: 599
Usually the impact of breaking views to partials is not so big (http://www.justinweiss.com/blog/2014/02/13/how-much-time-does-rendering-a-partial-really-take/). So it might be better to optimise other things like helpers and other calculation in partials.
In your situation i can suggest to cache rendered partial according to user. So if you meet multiple comments of one user you will use cached partial, avoiding recalculation of partial.
Something like this should work for you
cache "#{user.id}_avatar_with_nickname" do
# partial content
end
Upvotes: 2