Jonathan Bennett

To ViewComponent, or not to ViewComponent

I was recently asked by a client about when to create a new partial and when to create a new ViewComponent. My rules of thumb are:

  1. Use a ViewComponent for everything that isn’t a view (new.html.erb etc), and isn’t an ActiveModel partial (posts/_post.html.erb)
  2. ActiveModel partials should be moved into a ViewComponent as soon as you are tempted to add a view only concept to the model.
  3. Feel free to leave a one line partial that just renders a ViewComponent if it makes it easier to integrate with the normal Rails way of doing things.

This will leave you with a lot more ViewComponents, so make sure you keep them organized!