[Ruby on Rails] Convenient helper method

Introduction

About helper method, I read Perfect Ruby on Rails [enhanced revised edition] and summarize what I personally learned. ..

What is a helper method?

A handy method to help you develop with view templates. Some generate HTML and some generate URLs.

Path and URL helper methods

If the path starting with / excluding the domain and port with the Prefix value + _path when displaying the routing is set to the Prefix value + _url, the complete URL including the domain etc. is returned. If the path has a fluctuating value such as ": id", specify the value in hash format as an argument. Used when specifying the path or URL with url_for, link_to, or redirect_to.

In the case of the following routing, the following path and URL are returned.

routing

    users GET    /users(.:format)             users#index
     user GET    /users/:id(.:format)         users#show
edit_user GET    /users/:id/edit(.:format)    users#edit

** Return path or URL **

url_for Helper method for building web application paths. You can use the routing definition without writing the string directly. Use it when you just want to get the URL.

url_for(users_path)
#=> /users

link_to link_to is a helper method that generates an a tag. Use when you want to set a link on the screen by utilizing the definition on the routing with _path or _url.

link_to('Edit', edit_user_path(id: 1))
#=> <a href="/users/1/edit">Edit</a>

time_ago_in_words A helper method that makes it easy for humans to see how much space there is between a certain time and the current time. It seems that it can be used in a service that displays the last login date and time.

time_ago_in_words(Time.current)
#=> less than a minute
time_ago_in_words(Time.current + 3.days)
#=> 3 days

number_with_delimiter A helper method that separates long numbers into commas. You can optionally change the delimiter.

number_with_delimiter 1234567890
#=> 1,234,567,890
number_with_delimiter 1234567890, delimiter: '@'
#=> 1@234@567@890

Rails has various other helper methods, so before you implement it yourself, [Rails Guide](https://railsguides.jp/action_view_overview.html#action-view%E3%81%AE% E3% 83% 98% E3% 83% AB% E3% 83% 91% E3% 83% BC% E3% 83% A1% E3% 82% BD% E3% 83% 83% E3% 83% 89% E3% 81% AE% E6% A6% 82% E8% A6% 81) seems to be a good reference.

Proprietary helper method

If the Rails built-in helper method doesn't suit your needs, implement the helper method yourself. Modules for defining helper methods are defined under the directory "app / helpers". File for helper corresponding to the controller such as "app / helpers / application_helper.rb" if it is used for the whole application, and "app / helpers / users_helper.rb" if it is used for each controller. prepare.

Example) Helper method to convert full-width English characters to half-width

app/helper/application_helper.rb


module ApplicationHelper
  def to_hankaku(str)
    str.tr('A-Za-z', 'A-Za-z')
  end
end
to_hankaku('Ruby')
#=> Ruby

reference

Perfect Ruby on Rails [Augmented and Revised Edition]

Recommended Posts

[Ruby on Rails] Convenient helper method
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby on Rails] NoMethodError undefined method `devise_for'error resolution
Ruby on Rails address automatic input implementation method
[Ruby on Rails] How to use session method
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Ruby] Notes on gets method
Ruby on rails learning record -2020.10.06
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Method summary to update multiple columns [Ruby on Rails]
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] Search function (model, method selection formula)
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] yarn install --check-files
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
[Ruby on Rails] undefined method ʻid'for nil: NilClass error resolution method
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails record search, create if not find_or_create_by method
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
rails method
[Ruby on Rails] Stop "looping until ..."
[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Let's summarize "MVC" of Ruby on Rails
Ruby on Rails model creation / deletion command
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails in Visual Studio Codespaces
[Ruby on Rails] Follow function implementation: Bidirectional