Best practices for helpers #2393
-
I've been using ViewComponents for a while and have got into the habit of using a helper to make calling them a bit more compact in views, so <%= render SomeComponent.new(some: argument) %> becomes <%= render_some_component(argument) %> One sees this suggested in blog posts. I've usually done this in the controller, so def render_some_component(argument)
view_context.render SomeComponent.new(some: argument)
end
helper_method :render_some_component which works just fine (though you do have the controller context, so there is the temptation to use that context in the body of the helper, which would be a moral failing). I recently had occasion to make a component which make essential use of a block, and my usual controller helper printed output before where I was expecting it to. The full details in this SO question. So I think the problem was that I had defined this in the controller and used a So my question: is there a best practice for such helper wrappers? If so, might that be added to documentation? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@noproblemwiththat I see you've closed this discussion, but here is my opinion: I don't feel that these helpers are a net-positive. While they save some keystrokes, they add indirection that can make code harder to read. As such, we do not provide them in the framework nor encourage them in any of our documentation. |
Beta Was this translation helpful? Give feedback.
@noproblemwiththat I see you've closed this discussion, but here is my opinion: I don't feel that these helpers are a net-positive. While they save some keystrokes, they add indirection that can make code harder to read.
As such, we do not provide them in the framework nor encourage them in any of our documentation.