Home Is there any design pattern for using helper methods in models in Rails?
Reply: 2

Is there any design pattern for using helper methods in models in Rails?

Zainb Sallahuddin
1#
Zainb Sallahuddin Published in 2018-01-08 13:48:48Z

Is there any design pattern in Rails by which i can use helper methods in models, without using include ActionView::Helpers?

Peter Andersen
2#
Peter Andersen Reply to 2018-01-08 16:54:27Z

You can for example call one helper at a time if you want.

ApplicationController.helpers.my_helper_method

user2453676
3#
user2453676 Reply to 2018-01-08 17:22:41Z

ActionView::Helpers are intended to be used in your view code. If you have code that you want to use in you model and view, I would recommend creating a helper method in your model.

If you want to create a method that is used in multiple models, I would use concerns. Concerns are used to extract common chunks of code from models to DRY them up. Look here for more info

  • How to use concerns in Rails 4
  • http://api.rubyonrails.org/classes/ActiveSupport/Concern.html

An alternative to concerns is to create a superclass for common models to inherit from where you can add helper methods

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316505 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO