DEV Community

Cover image for Helpers in Ruby on Rails
Toqeer Abbas
Toqeer Abbas

Posted on

5

Helpers in Ruby on Rails

In this post i will discuss about helpers. I will cover all questions related to helpers in rails.

Why we need helpers?
A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods

How we can write helpers?

  • Create a new file under app/helpers.
  • Name it something like user_helper. rb.
  • Add a new module which matches the file name
`# app/helpers/user_helper.rb
module UserHelper
  def format_name(user)
    if user.gender == "M"
      "Mr. #{user.name}"
    else{% embed  %}
      "Ms. #{user.name}"
    end
  end
end`
Enter fullscreen mode Exit fullscreen mode

According to me helpers used in views. Never write your logics in views because that's like mess with views. your logics should be seprate from your views. For learn more watch this video

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay