Reputation: 7200
I wanted to get a general idea from other RoR coders out there as to the benefits of using partials if the code that the partials are pointing to are only a few lines (5-10 lines). By using Partials wouldn't it make it more complex in terms of going to files to view what is in the partials instead of seeing what is in the file itself?
This thought came up while trying to decide whether to use partials or not in my application.html.erb under my views/layout directory. I want to produce elegant code and the structure. Thanks for all your help.
Upvotes: 1
Views: 130
Reputation: 15126
Partials are very similar to functions/methods. When your function starts getting long, you break it into multiple functions to make things clearer. When some bit of code appears in more than one function, you refactor that bit into a separate function.
Sometimes you pass a container into a function and make it iterate over it, other times it's better to write a function that handles just one element. It's the same with partials.
Upvotes: 3
Reputation: 9895
I try to use partials whenever I feel it will clean up my code. Especially if you are re-using the code in any other section of your site. I often will put loops into partials too.
User.all.each do |user|
render :partial => do_stuff, :locals => {:user => user}
end
This way, if I ever need to change this, it's easy to find, and the code is separated from my other layouts/views. It's good to separate different features or aspects of your views into their own files. Although you will have more files, it should greatly assist you in the long run.
If someone asks you for a hotfix, you may have a better idea of where to go to make your change.
A great IDE also helps for navigation through your methods and whatnot. Rubymine is excellent for this.
I hope this helped some.
Upvotes: 2