Helpers Are Best at Exposing Global UI State
Learn why helpers are the best at exposing the global UI and generating the markup.
We'll cover the following...
Rails built-in helpers format data for the view or generate markup or both. If our app needs to do this, helpers are a good solution. We are also likely to need to implement view logic based on globally-stored context or state, such as the logged-in user and their authorizations. Helpers are great for this.
Global UI logic and state
As we mentioned above, almost every Rails app has a current_user
method that exposes the logged-in user. It’s common to require this on many views, either to access user-specific information or to check that the user is authorized to view different aspects of the UI. Setting a @current_user
instance variable in every controller method would be cumbersome.
Another example is feature flags. We might be rolling out a new feature to a subset of users and want to modify the UI for only those users. We may need to check if the user ...