When to use helpers instead of Partials

In the rails application, in what situation would you use partial, and when will you use the helper? I find both very similar, as they represent fragments of the markup.

Is there a convention around this? which is the "way of rails" to use them?

Thank!

+41
ruby-on-rails partials helper
Dec 30 '09 at 1:42
source share
6 answers

My guess is that my rule of thumb is to use an assistant to create a single display unit β€” for example, a range containing a link, and use a partial one to build a more complex display unit consisting of more than one display unit β€” like grid or menu.

+28
Dec 30 '09 at 1:53
source share

Partial is a presentation fragment, a presentation fragment that is useful in several places and pulled out to remove duplication. However, the bottom line is that presentations, whether standalone or partial, are for presentation.

As you know, controllers are designed to handle logic. However, it is inevitable that you will need some logical processing when presenting the view. So, for example, if you have a part of the presentation that is accessible only to administrators, you can extract this logic to the assistant and keep the presentation "pure" and "presentation only". Helpers will inevitably contain presentation code - html tags, etc., But this is a by-product of their use, and not their main function.

You can also combine two - partial for the presentation of the administrator and another for the presentation of the user and an assistant with logic to determine which one is displayed in a particular situation.

Just my $ .02.

+17
Dec 30 '09 at 2:55
source share

Other answers constitute a conceptual consensus on the use of assistants versus partial. For further reading, consider the following:

Basecamp dev doing the thing to minimize HTML in helpers http://37signals.com/svn/posts/1108-what-belongs-in-a-helper-method

Viget dev tested each and found that helpers are faster than partial http://www.viget.com/extend/helpers-vs-partials-a-performance-question/

+7
Mar 03 '11 at 7:28
source share

I use helpers when the code is likely to be used again in other projects, and partial code specific to the project.

+2
Dec 30 '09 at 2:38
source share

I use partials as subtopics (i.e. something with a lot of markup that is used over and over, like a blog post), and helpers to handle more logical things (divs that are visible only to administrators for an instance).

+2
Dec 30 '09 at 2:45
source share

Particles are part of a view that can have a bunch of HTML code along with ruby ​​code. You can also use partial elements in the controller in the rendering method.

In the case of helpers, you can have methods for short HTML. You can use some of the calculation methods that you want to use in your view file.

0
Mar 03 2018-11-11T00:
source share



All Articles