An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
November 20, 2022 06:56 pm GMT
Original Link: https://dev.to/toqeer__abbas/helpers-in-ruby-on-rails-1icl
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.rbmodule UserHelper def format_name(user) if user.gender == "M" "Mr. #{user.name}" else{% embed %} "Ms. #{user.name}" end endend`
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
Original Link: https://dev.to/toqeer__abbas/helpers-in-ruby-on-rails-1icl
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To