Rails Helper complete guide

 Hi, This is Shubham Mishra from India,  this is the part of Ruby on Rails exploration journey.  In this post we will discuss about the Rails Helper method and its usage in the rails view and also its access in the terminal or controller. Even though we can access rails helper methods in the controller, but it is highly recommended not to use it, since it violets the rails architecture guideline. We will look how we can use existing rails helper methods, create custom rails helper and how to use it and also will look how to make use of rails helper defined outside the scope of ApplicationHelper. We will also explore some of the helpers provided by the Rails.


ruby on rails helper method - Shubham mishra



If you are not aware of what is rails helper, Well in the ROR, in the view we may write some conditions or operations but it will make the view more complex and hence make the view dirty, and not only that sometimes we may have to write same operations at multiple places in the view, then why not to shift the logical code somewhere else and refer that new method at all the places where complex code were written, that is what Rails Helper.

First of all you must be know that the helpers will be present in app/helpers directory. And the base class for all the helpers will be ApplicationHelper which will be present in application_helper.rb

I think lets begging with the code and then will understand why and where to write that code, since it will be easier to understand.


Now, lets understand each line in the above code

In rails there are some predefined helper methods are available, which you can directly use in the view. Let suppose we want to show user friendly time or days from a timestamp, then rails provide a helper method, time_ago_in_words. As, you can read in the code we can call that method in the view,

time_ago_in_words(Time.now) #In the .haml file

<%= time_ago_in_words(Time.now) %> #In the .erb file

Sometimes we may want to check the helper methods in that case we can do so as mentioned in the code, through referring the helper class. In this case as the helper is belongs to application helper class hence we have called with ActionController::Base.helpers

In most of the time it happens that we have to define our own helper classes, then the approach is as simple as that,

  • Identify the model for which you want to declare the helper method, for better structuring of the project, let suppose I want a helper for user's profile view where I want to show 'Jr.' or 'Sr.' on the basis of age of the person. So I will create a helper for User if not exist. Note it that you can write helper method in the ApplicationHelper for all your requirement but that will make your codebase unstructured, so I highly recommend to create respective helper class and to mess up the application helper class.
  • Now see the code I have created user_helper.rb file, and created class UserHelper
  • In the UserHelper, I have defined the required method, and then as shown in the last line you may use the helper method. Now, the same helper method can be reused at many places by just calling it and passing the age value as Integer.
Ok, now let's see some more complex use cases such as, sometimes we may have defined a helper method outside the scope of ApplicationHelper, Then in that case we can include that helper in the ApplicationHelper module.


Well i would suggest you to go through the rails official guide for many existing helpers, Believe me there are many superb helper methods available, one among those you can see below (For generating a date select that defaults to the date provided, In our case 6 days after today ),


= select_date(Time.today + 6.days)

The output is below, (In my case, the date I recorded is 25th Sep 2020, hence the date 1st Oct 2020 is selected as 6 days is passed in the argument)


ruby on rails date select helper - shubham Mishra


As, above you may enjoy many other view helper methods provided by rails, It awesome isn't it?, If you find some useful rails helper then please comment it.

Now I think enough reading, I would suggest you to your rails and play with the code! 💻

Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.

Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.

My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.

Future of computer science technology discussed : A blog where me discussed about the future of computer science and new technologies which will change our way for looking to solve problems.

Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.

Comments

Popular posts from this blog

Rails Postgresql Query timeout set and skip during migration

Rails Sync Postgresql and Elasticsearch Database of a Model

Ruby on Rails configure CORS setting with rack cors gem